首页 > 数据库技术 > 详细

SQL 行转列

时间:2016-02-02 15:06:16      阅读:317      评论:0      收藏:0      [点我收藏+]

1. 利用 case when then

1 select DeptId,d.DepName, sum(case [Type] when 0 then CountVisit end) as ‘NewsType‘,
sum (case [Type] when 1 then CountVisit end) as ‘QuestionType‘,
sum (case [Type] when 2 then CountVisit end) as ‘PolicyType‘,
sum (case [Type] when 3 then CountVisit end) as ‘HomePage‘,
sum (case [Type] when 4 then CountVisit end) as ‘LoginCookie‘,
sum (case [Type] when 5 then CountVisit end) as ‘ShareType‘,
sum (case [Type] when 6 then CountVisit end) as ‘GalleryType‘ from
(select COUNT(Id) as CountVisit,DeptId,[Type] from VisitLog where AddTime between ‘" + startDate + "‘ and ‘" + endDate + "‘
group by DeptId,[Type])a join Department d on DeptId=d.Id group by DeptId,d.DepName

得到的结果是:

技术分享

2.

SQL 行转列

原文:http://www.cnblogs.com/sunShineJing/p/5177279.html

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