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

SQL 行转列

时间:2016-02-02 15:07:42      阅读:172      评论: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)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!