sqlserver2008查询某年每个月统计数据

;with cte_tot as(
select col_name,
sum((CAST(col_name AS numeric(18, 2)))) total,
convert(varchar(10),month(time_col))+'月' date

from table_name with(nolock)
  
  where convert(varchar(10),year(time_col)) = '2020' 
  
  group by col_name,month(time_col) )
 
select col_name, 
sum(case date when '1月' then total else 0 end) as '1月' ,
sum(case date when '2月' then total else 0 end) as '2月' ,
sum(case date when '3月' then total else 0 end) as '3月' ,
sum(case date when '4月' then total else 0 end) as '4月' ,
sum(case date when '5月' then total else 0 end) as '5月' ,
sum(case date when '6月' then total else 0 end) as '6月' ,
sum(case date when '7月' then total else 0 end) as '7月' ,
sum(case date when '8月' then total else 0 end) as '8月' ,
sum(case date when '9月' then total else 0 end) as '9月' ,
sum(case date when '10月' then total else 0 end) as '10月' ,
sum(case date when '11月' then total else 0 end) as '11月' ,
sum(case date when '12月' then total else 0 end) as '12月' 
from cte_tot where col_name is not null group by col_name

 

posted @ 2020-05-27 16:34  秋水秋色  阅读(1371)  评论(0编辑  收藏  举报