SQL 行转列
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL 行转列相关的知识,希望对你有一定的参考价值。
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 行转列的主要内容,如果未能解决你的问题,请参考以下文章