统计表:表头:文件类型、(所选年份)1-12月份
但是直接出去来的数据,无法直接在页面上展示:
执行语句:
select count(*) as totalCount,archiveType,month([archiveDate]) as yuefen from gd_documents where status!=3 and convert(varchar(20),archiveDate,23) like '2014%' group by archiveType,month([archiveDate]) order by archiveType, month([archiveDate])
这样取出来的数据,还需要在后台进行处理,处理的方式会比较麻烦。我就在想,有没有办法可以直接把最后一列的月份直接转换成行,同时类型一样的文件直接合并成一行,同时这一行的统计数据作为哪一月份的数据?
pivot...for
SQL提供了这个个方法,可以实现行转列。
得到的结果将是如下:
执行语句:
select * from (select archiveType,count(*) as totalCount,month([archiveDate]) as yuefen from gd_documents where status!=3 and convert(varchar(20),archiveDate,23) like '2014%' group by archiveType,month([archiveDate])) a pivot (max(totalCount) for yuefen in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) b