SQL行转列 静态?

转换前的 :
ID name sex num
1 tom 男 2
2 tom 男 3
3 tom 男 4
4 tom 男 5
这是转换后的结果
ID name sex num1 num2 num3 num4
1 tom 男 2 3 4 5



select MIN(id) as ID, name, sex, 
sum(case when num=2 then 2 end) as num2,
sum(case when num=3 then 3 end) as num3,
sum(case when num=4 then 4 end) as num4,
sum(case when num=5 then 5 end) as num5
from 表名 group by name,sex
原文地址:https://www.cnblogs.com/dlexia/p/4518793.html