sql按月汇总

来源:百度知道 编辑:UC知道 时间:2024/05/28 08:02:06
现有一sql server表A(id , year int(4) not null , month int(4) not null , money int(4) ) , year和month代表日期的年和月 , 现要按月汇总,按季汇总
请问怎样写sql语句
那对于2009年以外的年怎么办?

按月份
select sum(money)
from A
group by Year,month
按季度
select ceil(month/3),sum(money)
from A
group by Year,ceil(month/3)

按月汇总:
select sum(money),month from table group by month;

按季汇总:
select sum(money) from table where month in (1,2,3)
union
select sum(money) from table where month in (4,5,6)
union
select sum(money) from table where month in (7,8,9)
unoin
select sum(money) from table where month in (10,11,12);

1、按月汇总:
SELECT year, month,
SUM(money)
FROM goods
GROUP BY year, month
ORDER BY year, month