如何用sql生成固定时间间隔的统计表[转]

 来源: http://bbs.chinaunix.net
  • 核心提示:有表Incoming_call,要统计A时到B时每隔X时长的话务量,只用SQL,我想,应该可以写出来吧? 欢迎高手试一试 不明白你说的意思。 表Incoming_call如下: time action 00:01 call in 00:02 call in 00:05 call in 00:08 call in 00:12 call in 00:15 call in 00:18 call in 0.....

有表Incoming_call,要统计A时到B时每隔X时长的话务量,只用SQL,我想,应该可以写出来吧?
欢迎高手试一试

不明白你说的意思。

表Incoming_call如下:
time    action
00:01 call in
00:02 call in
00:05 call in
00:08 call in
00:12 call in
00:15 call in
00:18 call in
00:21 call in
00:22 call in
00:25 call in
00:28 call in
......
假如想展现的是从 00:00----01:00 每隔10分钟的call数:
interval            call_times
00:00---00:10  4
00:10---00:20  3
00:20---00:30  4
......
请教各专家用一个sql如何写。

select substr(to_char(time,'mi')),1,1) time ,sum(count(action)) from incoming_call
group by substr(to_char(time,'mi')),1,1)
试试


谢楼上指点。
假如是3分钟间隔呢?再假如时间间隔是指定的呢?
另外,时间也不只是1小时。INCOMING_CALL是做了个示例。
欢迎各位DX一试

自己找到方法了:
假定t0, t1, interval
select
t0 + ceil((time - t0) / interval), count(*)
where time between t0 and t1
group by t0 + ceil((time - t0) / interval)
这是个示意,还需要很多函数填空完整。这个还是经常用到的SQL,希望能给大家以启示

CU是大家自由讨论的地方,低水平的贴子固然令人发笑,可是也希望各位DX们不要一味打杀,让我等战战兢,汗不敢出。贴子就更不敢发了

补全了函数,假定间隔interv单位为分钟:
select  to_date('&t0','yyyy/mm/dd hh:mi:ss') + (a - 1)/1440*&interv ,
to_date('&t0','yyyy/mm/dd hh:mi:ss') + a/1440*&interv, count(*)
from (
select
decode(ceil((time - to_date('&t0','yyyy/mm/dd hh:mi:ss'))*1440/&interv), 0,1,
ceil((time - to_date('&t0','yyyy/mm/dd hh:mi:ss'))*1440/&interv)) a

from incoming_call
where time between to_date('&t0','yyyy/mm/dd hh:mi:ss')
and to_date('&t1','yyyy/mm/dd hh:mi:ss')
)
group by to_date('&t0','yyyy/mm/dd hh:mi:ss') + (a - 1)/1440*&interv ,
to_date('&t0','yyyy/mm/dd hh:mi:ss') + a/1440*&interv;
原文地址:https://www.cnblogs.com/craig/p/1205475.html