SQL按时间分段分组统计数据
sql中按照时间分段分组,显示及统计分段数据,最后获取总行数:
文章图片
下面sql条件中48表示时间段数(一天48个0.5小时即半小时分段统计,以此类推修改);--注释了查询条件即只统计错误或统计时间限制(hh24miss)内的数据;最后0和500分别为分页起止数。
查询行数据
select *
from (select rownum as rn, a.*
from (select to_char((d.timepoint - 1 / 48),'yyyy-mm-dd hh24:mi:ss') starttime,--临时表d存储了分好了的今天的各时间段
to_char(timepoint,'yyyy-mm-dd hh24:mi:ss') endtime,
t.jobid,
sum(count) updateTimes,
sum(decode(status, 2, 1, 0)) failTimes,
sum(cost) totalCost,
sum(rowsize) updateSize
from kdgs_realtime_log t,
(select to_date('20171017', 'YYYYMMDD') + 1 -
(level - 1) / 48 timepoint
from dual
connect by level <= 48) d
where t.jobid = 1000009
and t.kddate = 20171017
and to_date(t.updatetime, 'YYYYMMDDHH24MISS') <
d.timepoint
and to_date(t.updatetime, 'YYYYMMDDHH24MISS') >=
d.timepoint - 1 / 48
--AND t.status = '2'
--and substr(t.updatetime,9,6) >=161010
--and substr(t.updatetime,9,6) <=183000
group by d.timepoint, t.jobid
order by starttime) a
where rownum <= 500) tt
where tt.rn > 0
总行数:
select count(1)
from (select count(1)
from kdgs_realtime_log t,
(select to_date('20171017', 'YYYYMMDD') + 1 - (level - 1) / 48 timepoint
from dual
connect by level <= 48) d
where t.jobid = 1000009
and to_date(t.updatetime, 'YYYYMMDDHH24MISS') < d.timepoint
and to_date(t.updatetime, 'YYYYMMDDHH24MISS') >=
d.timepoint - 1 / 48
--AND t.status = '2'
--and substr(t.updatetime,9,6) >=161010
--and substr(t.updatetime,9,6) <=183000
group by d.timepoint, t.jobid)
【SQL按时间分段分组统计数据】
推荐阅读
- 20170612时间和注意力开销记录
- 2021-02-17|2021-02-17 小儿按摩膻中穴-舒缓咳嗽
- 时间老了
- 牛人进化+|牛人进化+ 按自己的意愿过一生
- Eddy小文
- C语言中的时间函数clock()和time()你都了解吗
- 山香|山香 善思 智学访谈
- 请给时间一点时间
- 全过程工程咨询——时间管理(12)
- 最有效的时间管理工具(赢效率手册和总结笔记)