原文:
sql server 查询本年的每个月的数据
一、以一行数据的形式,显示本年的12月的数据,本示例以2017年为例,根据CreateDate字段判断,计算总和,查询语句如下:
select sum(case when datepart(month,CreateDate)=1 then 1 else 0 end) as '1月', sum(case when datepart(month,CreateDate)=2 then 1 else 0 end) as '2月', sum(case when datepart(month,CreateDate)=3 then 1 else 0 end) as '3月', sum(case when datepart(month,CreateDate)=4 then 1 else 0 end) as '4月', sum(case when datepart(month,CreateDate)=5 then 1 else 0 end) as '5月', sum(case when datepart(month,CreateDate)=6 then 1 else 0 end) as '6月', sum(case when datepart(month,CreateDate)=7 then 1 else 0 end) as '7月', sum(case when datepart(month,CreateDate)=8 then 1 else 0 end) as '8月', sum(case when datepart(month,CreateDate)=9 then 1 else 0 end) as '9月', sum(case when datepart(month,CreateDate)=10 then 1 else 0 end) as '10月', sum(case when datepart(month,CreateDate)=11