原数据:
pivot:
pivot(聚合函数(要转成列值的列名)
for 要转换的列
in(目标列名)
)
select *
from sc
pivot(sum(score)
for cno in('1' as 语文, '2' as 数学, '3' as 英语, '4' as 历史));
case……when……
select sno,
sum(case when cno='1' then score else 0 end)as 语文,
sum(case when cno= '2' then score else 0 end)as 数学,
sum(case when cno='3' then score else 0 end)as 英语,
sum(case when cno='4' then score else 0 end)as 历史 from sc
group by sno