MyBatis mysal 日报表,月,年报表的统计

时间:2021-04-06 17:33:17

mysql 按日、周、月、年统计sql语句整理,实现报表统计可视化

原文地址:http://blog.csdn.net/u010543785/article/details/52354957

最近在做报表统计,前端可视化插件用的是Echarts,个人认为是目前免费插件里的翘楚http://echarts.baidu.com/demo.html#pie-doughnut例子演示

现在重点如何把后台数据源搞定,我采用的是mybatis,所以重点是怎么把sql语句写好。

– –按日

SELECT COUNT(*),DATE(CreateTime) FROM t_voipchannelrecord WHERE YEAR(CreateTime)='2016' GROUP BY DAY(CreateTime)
  • 1

MyBatis mysal 日报表,月,年报表的统计

– –按周

 SELECT COUNT(*),WEEK(CreateTime) FROM t_voipchannelrecord WHERE MONTH(CreateTime) = '8' GROUP BY WEEK(CreateTime) 
  • 1

–周一到周五每天的统计结果

 SELECT COUNT(*),DAYNAME(CreateTime) FROM t_voipchannelrecord WHERE YEAR(CreateTime) = '2016' GROUP BY DAYNAME(CreateTime) 
  • 1

MyBatis mysal 日报表,月,年报表的统计

–统计本周数据

SELECT COUNT(*) FROM t_voipchannelrecord WHERE MONTH(CreateTime) =
MONTH(CURDATE()) AND WEEK(CreateTime) = WEEK(CURDATE())
  • 1
  • 2

–按月统计

SELECT COUNT(*),MONTH(CreateTime) FROM t_voipchannelrecord WHERE YEAR(CreateTime) = '2016' GROUP BY MONTH(CreateTime) 
  • 1

MyBatis mysal 日报表,月,年报表的统计

–统计本月数据

SELECT COUNT(*) FROM t_voipchannelrecord WHERE MONTH(CreateTime) =
MONTH(CURDATE()) AND YEAR(CreateTime) = YEAR(CURDATE())
  • 1
  • 2

–按季统计

SELECT COUNT(*),QUARTER(CreateTime) FROM t_voipchannelrecord WHERE YEAR(CreateTime) = '2016' GROUP BY QUARTER(CreateTime) 
  • 1

–按年统计


SELECT COUNT(*),YEAR(CreateTime) FROM t_voipchannelrecord GROUP BY YEAR(CreateTime)
  • 1
  • 2

–时间段(该段参考:出处)

N天内记录
WHERE TO_DAYS(NOW()) - TO_DAYS(时间字段) <= N
当天的记录
where date(时间字段)=date(now())

where to_days(时间字段) = to_days(now()); 查询一周:
select * from table where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(column_time); 查询一个月:
select * from table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= date(column_time); 查询'06-03'到'07-08'这个时间段内所有过生日的会员:
Select * From user Where
DATE_FORMAT(birthday,'%m-%d') >= '06-03' and DATE_FORMAT(birthday,'%m-%d')
<= '07-08'; 统计一季度数据,表时间字段为:savetime
group by concat(date_format(savetime, '%Y '),FLOOR((date_format(savetime, '%m ')+2)/3))

select YEAR(savetime)*10+((MONTH(savetime)-1) DIV 3) +1,count(*)
from yourTable
group by YEAR(savetime)*10+((MONTH(savetime)-1) DIV 3) +1; N天内记录 WHERE TO_DAYS(NOW()) - TO_DAYS(时间字段) <= N 当天的记录 where date(时间字段)=date(now()) 或 where to_days(时间字段) = to_days(now()); 查询一周: select * from table where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(column_time); 查询一个月: select * from table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= date(column_time); 查询'06-03'到'07-08'这个时间段内所有过生日的会员: Select * From user Where DATE_FORMAT(birthday,'%m-%d') >= '06-03' and DATE_FORMAT(birthday,'%m-%d') <= '07-08'; 统计一季度数据,表时间字段为:savetime group by concat(date_format(savetime, '%Y '),FLOOR((date_format(savetime, '%m ')+2)/3)) 或 select YEAR(savetime)*10+((MONTH(savetime)-1) DIV 3) +1,count(*) from yourTable group by YEAR(savetime)*10+((MONTH(savetime)-1) DIV 3) +1;