create table t(num int,s_ID varchar(10),s_Card varchar(10),s_Qty int)
insert into t select 1,'01','a',20
insert into t select 2,'01','c',10
insert into t select 3,'01','d',22
insert into t select 4,'02','a',23
insert into t select 5,'02','b',2
insert into t select 6,'02','d',12declare @s varchar(8000)
set @s='select s_ID'select @s=@s+','+s_Card+'=sum(case s_Card when '''+s_Card+''' then s_Qty else 0 end)'
from t group by s_Card order by s_Cardset @s=@s+' from t group by s_ID order by s_ID'exec(@s)/*
s_ID a b c d
---------- ----------- ----------- ----------- -----------
01 20 0 10 22
02 23 2 0 12
*/drop table t
insert into t select 1,'01','a',20
insert into t select 2,'01','c',10
insert into t select 3,'01','d',22
insert into t select 4,'02','a',23
insert into t select 5,'02','b',2
insert into t select 6,'02','d',12declare @s varchar(8000)
set @s='select s_ID'select @s=@s+','+s_Card+'=sum(case s_Card when '''+s_Card+''' then s_Qty else 0 end)'
from t group by s_Card order by s_Cardset @s=@s+' from t group by s_ID order by s_ID'exec(@s)/*
s_ID a b c d
---------- ----------- ----------- ----------- -----------
01 20 0 10 22
02 23 2 0 12
*/drop table t
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货