select uname, type, sum(num) from table group by uname, type (假设三列分别叫uname, type和num)
use Itcast2013 select * from T_Score create table T_Csdn ( name nvarchar(10) not null, ID char(2) not null, number int not null ) insert into T_Csdn(name,ID,number) select N'张三','a',10 union all select N'张三','b',5 union all select N'张三','a',20 union all select N'李四','b',10 union all select N'李四','a',10 union all select N'张三','b',5 select name as 姓名, sum((case ID when 'a' then number else 0 end)) as 'a' , sum((case ID when 'b' then number else 0 end)) as 'b' from T_Csdn group by name
(假设三列分别叫uname, type和num)
select * from T_Score
create table T_Csdn
(
name nvarchar(10) not null,
ID char(2) not null,
number int not null
)
insert into T_Csdn(name,ID,number)
select N'张三','a',10 union all
select N'张三','b',5 union all
select N'张三','a',20 union all
select N'李四','b',10 union all
select N'李四','a',10 union all
select N'张三','b',5 select
name as 姓名,
sum((case ID when 'a' then number else 0 end)) as 'a' ,
sum((case ID when 'b' then number else 0 end)) as 'b'
from T_Csdn
group by name