日期:2014-05-18  浏览次数:20525 次

按月分组统计问题
created_date 是 表 A 的一个日期字段,amount和quan是表A的金额跟数量字段,我想按月分组统计金额跟数量(1\2\3\4等自然月统计),SQL怎么写?

------解决方案--------------------
SQL code

--按月求和
select month(created_date)  as month,sum(amount),sum(quan)
  from tb
    group by month(created_date)

------解决方案--------------------
SQL code

--应该把年份也考虑进来吧,如果不用那么2楼就是对的了
select year(created_date) as year,month(created_date)  as month,sum(amount),sum(quan)
from tb
group by year(created_date),month(created_date)