首页 > 数据库技术 > 详细

SQL行转列 静态?

时间:2015-05-21 10:26:26      阅读:247      评论:0      收藏:0      [点我收藏+]
转换前的 :
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

SQL行转列 静态?

原文:http://www.cnblogs.com/dlexia/p/4518793.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!