create table tb(id int, classid int, value int, datatime datetime)
insert into tb
select 1, 1 , 233 , '2005-4-1'
union all select 1 , 1 , 222 , '2005-4-2'
union all select 1 , 2 , 234 , '2005-4-1'
union all select 1 , 2 , 244 , '2005-4-2'declare @s1 nvarchar(4000)
select @s1=''select @s1=@s1+','+quotename('(id,classid)('+rtrim(id)+','+rtrim(classid)+')')+'=sum(case when id='+rtrim(id)+' and classid='+rtrim(classid)+' then value end)' from tb group by id,classid
select @s1='select datatime' + @s1 + ' from tb group by datatime'
exec(@s1)drop table tb
insert into tb
select 1, 1 , 233 , '2005-4-1'
union all select 1 , 1 , 222 , '2005-4-2'
union all select 1 , 2 , 234 , '2005-4-1'
union all select 1 , 2 , 244 , '2005-4-2'declare @s1 nvarchar(4000)
select @s1=''select @s1=@s1+','+quotename('(id,classid)('+rtrim(id)+','+rtrim(classid)+')')+'=sum(case when id='+rtrim(id)+' and classid='+rtrim(classid)+' then value end)' from tb group by id,classid
select @s1='select datatime' + @s1 + ' from tb group by datatime'
exec(@s1)drop table tb
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货