22,207
社区成员
发帖
与我相关
我的任务
分享
if object_id('tb') is not null
drop table tb
go
create table tb(货号 int,颜色 varchar(50),规格 varchar(50),销量 int)
insert into tb select 1001,'黑色','XX',100
insert into tb select 1001,'黑色','xxl',100
insert into tb select 1002,'白色','XX',20
insert into tb select 1002,'黑色','xxl',30
select 货号,颜色,
sum(case when 规格='xx' then 销量 else 0 end ) as 'xx',
sum(case when 规格='xxl' then 销量 else 0 end ) as 'xxl'
from tb
group by 货号,颜色
order by 货号