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 @   秋水秋色  阅读(1400)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
历史上的今天:
2018-05-27 MySQL系列之三查询优化
点击右上角即可分享
微信分享提示