|
发表于 2020-6-2 18:15:01
|
显示全部楼层
if object_id('pubs..tb') is not null
drop table tb
go
create table tb
(
季度 int,
Fitem varchar(10),
数量 int
)
insert into tb(季度,Fitem,数量) values(1, 'A', 20)
insert into tb(季度,Fitem,数量) values(2, 'B', 80)
insert into tb(季度,Fitem,数量) values(3, 'A', 50)
insert into tb(季度,Fitem,数量) values(4, 'B', 50)
select fitem ,
sum(case when 季度 = 1 then 数量 else null end) as [1],
sum(case when 季度 = 2 then 数量 else null end) as [2],
sum(case when 季度 = 3 then 数量 else null end) as [3],
sum(case when 季度 = 4 then 数量 else null end) as [4]
from tb
group by Fitem
drop table tb
/*result
fitem 1 2 3 4
---------- ----------- ----------- ----------- -----------
A 20 NULL 50 NULL
B NULL 80 NULL 50
(所影响的行数为 2 行)
*/ |
|