多语言展示
当前在线:922今日阅读:167今日分享:27

oracle 按小时统计表中各小时的数据量条数

一个表定时程序跑,想统计一天内,按小时统计该表中一天内各小时的数据量,该如何做呢?
工具/原料

plsql

方法/步骤
1

以下,例如,统计2016年5月06号一天各个小时内的数据量有多少,表名:TASK_RESULT, 统计按照stat_date字段的时间统计

2

select s.stat_date, s.data_num  from (select to_char(stat_date, 'yyyy/mm/dd hh24') || '点' as stat_date,               count(1) as data_num          from TASK_RESULT t         where t.stat_date >= to_date('20160506', 'yyyymmdd')           and t.stat_date < to_date('20160507', 'yyyymmdd')         group by to_char(stat_date, 'yyyy/mm/dd hh24') || '点') s

3

注意,时间格式要用yyyy.mm.dd hh24,不要用hh,那样早上九点和晚上九点的就统计到一起了,因为hh是12小时制,统计结果,如下图:

4

按15分钟统计,如下SQL:select count(*),       (case floor((to_char(stat_date, 'mi')) / 15)         when 0 then          to_char(stat_date, 'yyyy.mm.dd hh24') || ':00:00'         when 1 then          to_char(stat_date, 'yyyy.mm.dd hh24') || ':15:00'         when 2 then          to_char(stat_date, 'yyyy.mm.dd hh24') || ':30:00'         when 3 then          to_char(stat_date, 'yyyy.mm.dd hh24') || ':45:00'       end) as stat_date  from TASK_RESULT s   where s.stat_date >= to_date('20160506', 'yyyymmdd')           and s.stat_date < to_date('20160507', 'yyyymmdd') group by stat_date

5

统计结果,如下图:

注意事项

注意,时间格式要用yyyy.mm.dd hh24,不要用hh,那样早上九点和晚上九点的就统计到一起了,因为hh是12小时制

推荐信息