最近有个需求,是这样的:
当时看到这个需求的第一反应是挺简单的,应该一条sql就能统计完,后面发现真正实现的时候,用sql会面临一些些问题,比如说在某一些时间段,可能会没有数据,这样就导致某一些时间段统计出来可能没有数据。又因为时间比较紧,懒得折腾,所以先采用java + sql的方式来统计,后面有时间再来研究研究纯sql统计。
- 根据时间(小时统计出每个小时的扫码量)
--查询当日的亮码统计 SELECt count(*) AS num, date_format( insert_time, '%H' ) AS timeZones FROM gtm_qrcode_log WHERe to_days( insert_time ) = to_days( now()) AND type = '0' GROUP BY timeZones
- 通过java代码进行统计
private ListinitDataList(List timeZonesVOList) { List list = new ArrayList<>(); for (int i = 0; i < 12; i++) { BigDataShowColorCodeTimeZonesVO timeZonesVO = new BigDataShowColorCodeTimeZonesVO(); // 开区间 int openInterval = i * 2; // 闭区间 int closedInterval = openInterval + 2; // 初始数量 int num = 0; StringBuffer stringBuffer = new StringBuffer(); if (openInterval < 8) { stringBuffer.append("0").append(openInterval).append(":00-").append("0").append(closedInterval).append(":00"); } else if (openInterval == 8) { stringBuffer.append("0").append(openInterval).append(":00-").append(closedInterval).append(":00"); } else { stringBuffer.append(openInterval).append(":00-").append(closedInterval).append(":00"); } timeZonesVO.setTimeZones(stringBuffer.toString()); for (BigDataShowColorCodeTimeZonesVO vo : timeZonesVOList) { Integer valueOf = Integer.valueOf(vo.getTimeZones()); if (valueOf >= openInterval && valueOf < closedInterval) { num += vo.getNum(); } } timeZonesVO.setNum(num); list.add(timeZonesVO); } return list; }
- 最终的数据效果
"timeZonesVOList":[
{
"num":126,
"timeZones":"00:00-02:00"
},
{
"num":90,
"timeZones":"02:00-04:00"
},
{
"num":85,
"timeZones":"04:00-06:00"
},
{
"num":325,
"timeZones":"06:00-08:00"
},
{
"num":568,
"timeZones":"08:00-10:00"
},
{
"num":1,
"timeZones":"10:00-12:00"
},
{
"num":486,
"timeZones":"12:00-14:00"
},
{
"num":224,
"timeZones":"14:00-16:00"
},
{
"num":0,
"timeZones":"16:00-18:00"
},
{
"num":0,
"timeZones":"18:00-20:00"
},
{
"num":0,
"timeZones":"20:00-22:00"
},
{
"num":0,
"timeZones":"22:00-24:00"
}
]



