紧急求助:SQL查询问题

shy06858 2009-03-09 03:05:36
我现在有一张表。如下,
编号 年月 数量
A01 2009-01 100
A01 2009-02 150
A02 2009-01 300
A02 2009-02 350

我现在想得到这样的一张结果表:

编号 1月(2009-01) 2月(2009-02)
A001 100 150
A002 300 350

请问该如何实现啊,谢谢各位大侠了....分不够再加啊!!!!
...全文
101 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
haibozhou1 2009-03-09
  • 打赏
  • 举报
回复
create table T_Test
(ID nvarchar(100),
Date nvarchar(100),
SumS int)

insert T_Test values('A01','2009-01',100)
insert T_Test values('A01','2009-02', 150)
insert T_Test values('A02','2009-01',300)
insert T_Test values('A02','2009-02',350)


select ID,Y200901=sum(case when Date='2009-01' then SumS end),
Y200902=sum(case when Date='2009-02' then SumS end)
from T_Test group by ID
shy06858 2009-03-09
  • 打赏
  • 举报
回复
感谢CCTV,感谢CSDN,更要感谢你 hongqi162失踪的月亮
看到你的答案后,使我对未来充满的信心!
rascalwm 2009-03-09
  • 打赏
  • 举报
回复
mark
lxf2000104 2009-03-09
  • 打赏
  • 举报
回复
create table #tbl([编号] nvarchar(10), [年月] nvarchar(10),[数量] int)
insert into #tbl
select 'A01','2009-01',100 union all
select
'A01','2009-02',150 union all
select
'A02','2009-01',300 union all
select
'A02','2009-02',350

declare @sql nvarchar(4000)
set @sql='select 编号'
select @sql=@sql+',isnull(sum(case 年月 when '''+年月+''' then 数量 else 0 end),0) as ['+substring(年月,7,1)+'月('+年月+')]'
from #tbl group by 年月 order by 年月
set @sql=@sql+' from #tbl group by 编号 order by 编号'

exec(@sql)
drop table #tbl
randomfeel 2009-03-09
  • 打赏
  • 举报
回复
随便写了一个,对应楼主给的数据,可以给出正确的答案了,如果楼主想适应所有年份、月份都可以查处正确结果,就需要自己改改了

表:test
no cdate num
A01 2009-01 100
A01 2009-02 150
A02 2009-01 300
A02 2009-02 350


select no,sum(case m when 1 then total else 0 end) as '一月',sum(case m when 2 then total else 0 end) as '二月'
from
(select month(cdate) as m,no,sum(num) as total from test group by no,month(cdate) ) DERIVEDTBL
group by no

hongqi162 2009-03-09
  • 打赏
  • 举报
回复
存储过程应该类似这样,你的问题就是标准的行列转换问题

declare @sql varchar(8000)
set @sql='select 编号'
select @sql=@sql+','+t.年月+'=isnull(sum(case 年月 when '''+t.年月+''' then 数量 end),0)'
from 表 group by 年月 order by 年月
set @sql=@sql+' from 表 t group by 编号 order by 编号'
print @sql
exec(@sql)
hxa165 2009-03-09
  • 打赏
  • 举报
回复
学习了
shy06858 2009-03-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 shy06858 的回复:]
楼上的谢谢你先,不过我看不到你写的这个啊,复杂
[/Quote]
不是看不到,而是看不懂
dd__dd 2009-03-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hongqi162 的回复:]
写个存储过程拼接成下面的sql就可以了

SQL codeselect 编号,
1月(2009-01)=isnull(sum(case course when '2009-01' then result end),0),
2月(2009-02)=isnull(sum(case course when '2009-02' then result end),0)
from 表
group by 编号
order by 编号
[/Quote]
up
hxa165 2009-03-09
  • 打赏
  • 举报
回复
学习了
shy06858 2009-03-09
  • 打赏
  • 举报
回复
楼上的谢谢你先,不过我看不到你写的这个啊,复杂
hongqi162 2009-03-09
  • 打赏
  • 举报
回复
写个存储过程拼接成下面的sql就可以了
select 编号,
1月(2009-01)=isnull(sum(case course when '2009-01' then result end),0),
2月(2009-02)=isnull(sum(case course when '2009-02' then result end),0)
from 表
group by 编号
order by 编号
shy06858 2009-03-09
  • 打赏
  • 举报
回复
来个可以帮助我的好人好吗?

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧