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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM