select month(Time),sum(Money) from 表名 where Year(Time)=2006 group by month(Time)
如果想要某个月没有记录值的为空或者0的话则可以用下述SQL语句,如果你有ID索引的话,执行速度上面还是不慢的
Select
(Select Sum(P_Money) From Pay_View Where Year(P_InTime)=2011 and Month(P_InTime)=1)AS 一月,
(Select Sum(P_Money) From Pay_View Where Year(P_InTime)=2011 and Month(P_InTime)=2)AS 二月,
(Select Sum(P_Money) From Pay_View Where Year(P_InTime)=2011 and Month(P_InTime)=3)AS 三月,
(Select Sum(P_Money) From Pay_View Where Year(P_InTime)=2011 and Month(P_InTime)=4)AS 四月,
(Select Sum(P_Money) From Pay_View Where Year(P_InTime)=2011 and Month(P_InTime)=5)AS 五月,
(Select Sum(P_Money) From Pay_View Where Year(P_InTime)=2011 and Month(P_InTime)=6)AS 六月,
(Select Sum(P_Money) From Pay_View Where Year(P_InTime)=2011 and Month(P_InTime)=7)AS 七月,
(Select Sum(P_Money) From Pay_View Where Year(P_InTime)=2011 and Month(P_InTime)=8)AS 八月,
(Select Sum(P_Money) From Pay_View Where Year(P_InTime)=2011 and Month(P_InTime)=9)AS 九月,
(Select Sum(P_Money) From Pay_View Where Year(P_InTime)=2011 and Month(P_InTime)=10)AS 十月,
(Select Sum(P_Money) From Pay_View Where Year(P_InTime)=2011 and Month(P_InTime)=11)AS 十一月,
(Select Sum(P_Money) From Pay_View Where Year(P_InTime)=2011 and Month(P_InTime)=12)AS 十二月
- 本文标题: SQL查询一年内12个月每个月的汇总SQL语句的写法
- 文章分类:【SQLServer】
- 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.