1、时间字段是datetime类型
2、需要用到中间表,表结构和数据链接下载
链接:https://pan.baidu.com/s/1tAFU4nm1p4CkSneXwceA_A
提取码:b3xi
3、sql
select CREATEDATE as YYmmdd, CONCAT(CREATEDATE,‘ ‘,d.i) YYmmddHH, d.i as HH, case when zscum is null then 0 else zscum end as zscum from ( select DATE_FORMAT(CREATEDATE,‘%Y-%m-%d‘) as CREATEDATE, SUM(CASE WHEN e.status = ‘1‘ THEN 1 ELSE 0 END ) AS zscum, HOUR ( CREATEDATE ) as hour from event_base_info e -- 自己的条件 where e.status=‘1‘ and e.STATE not in(‘3702820001‘) -- 时间 and DATE_FORMAT( CREATEDATE, ‘%Y-%m-%d‘ ) like ‘2019-06-04%‘ GROUP BY HOUR ( CREATEDATE ) ) o -- 中间表 RIGHT JOIN ( select i from num where i between ‘0‘ and ‘24‘ ) d on o.hour = d.i GROUP BY d.i
5、效果 (HH是小时,用了中间表)
如有疏漏,请指正!谢谢
原文地址:https://www.cnblogs.com/w-yu-chen/p/10976792.html
时间: 2024-11-13 09:33:14