SQL语句统计每天、每月、每年的数据

1、每年

select year(add_time) 年,sum(income) 销售合计 from 财务表
 group by year(add_time)

 2、每月

select year(add_time) 年,month(add_time) 月,sum(income) 销售合计 from 财务表
 group by year(add_time),month(add_time)

 3、每日

select year(add_time) 年,month(add_time) 月,day(add_time) 日,sum(income) 销售合计 from 财务表
 group by year(add_time),month(add_time),day(add_time)

 实例:查询本月所有天数的微信支付统计

select year(add_time) year,month(add_time) month,day(add_time) day, 
(select sum(income) from dt_payrecord where payment_id=5 and day(add_time)=day(a.add_time)) weixin_income 
 where datediff(month,add_time ,getdate())=0 
 group by year(add_time),month(add_time),day(add_time) order by day asc

 

posted @ 2017-11-22 14:39  microsoftzhcn  阅读(2310)  评论(0编辑  收藏  举报