码迷,mamicode.com
首页 > 数据库 > 详细

SQL行转列 静态?

时间:2015-05-21 10:27:33      阅读:126      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!