SQL查询实现时间段区间统计

问题描述:
表数据j结果如下:
serviceidcallernobegintimeendtime
serviceid业务类型,callerno手机号码,其中begintime和endtime均为date类型分别为通话开始时间和通话结束时间,现在要统计2009-6-1每三十分钟业务分布情况,包括通话次数和通话总时间,比如统计的区间格式为00:00:00-00:30:00,00:30:00-01:00:00........
解决方案:
该问题的难点主要在于三十分钟区间格式的处理,在这里我将通过构造一个整数表来实现:
SQL> create table test1(serviceid number,
2callerno number,
3begintime date,
4endtime date);
表已创建。
SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';

会话已更改。

SQL> select * from test1;

SEVICEIDCALLERNO BEGINTIMEENDTIME
---------- ---------- ------------------- -------------------
1138 2009-06-01 00:25:00 2009-06-01 00:26:10
2136 2009-06-01 00:10:00 2009-06-01 00:12:10
3135 2009-06-01 01:10:00 2009-06-01 01:14:10
SQL> select to_date('2009-06-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') +
2(n - 1) / 48 sec_begin,
3to_date('2009-06-01 00:00:00', 'yyyy-mm-ddhh24:mi:ss') + n / 48 sec_
end,
4count(callerno) callnum,
5nvl(sum(endtime - begintime),0) * 24 * 60 * 60 "calllength(s)"
6from test1 t,
7(select rownum n from dual connect by level < = 48) integers
8where t.begintime between
9to_date('2009-06-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') +
10(n - 1) / 48 and
11to_date('2009-06-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') + n / 48
12group by n;

SEC_BEGINSEC_ENDCALLNUM calllength(s)
------------------- ------------------- ---------- -------------
2009-06-01 00:00:00 2009-06-01 00:30:002200
2009-06-01 01:00:00 2009-06-01 01:30:001250
上面的结果只显示了存在呼叫信息的时间段的统计数据,而对于其他时间段则没有显示出来,可以通过外连接来显示一天中所有时间段的统计信息,具体实现如下:

SQL> select to_date('2009-06-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') +
2(n - 1) / 48 sec_begin,
3to_date('2009-06-01 00:00:00', 'yyyy-mm-ddhh24:mi:ss') + n / 48 sec_
end,
4count(callerno) callnum,
5nvl(sum(endtime - begintime),0) * 24 * 60 * 60 "calllength(s)"
6from test1 t right join
7(select rownum n from dual connect by level < = 48) integers
8on t.begintime between
9to_date('2009-06-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') +
10(n - 1) / 48 and
11to_date('2009-06-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') + n / 48
12group by n;

SEC_BEGINSEC_ENDCALLNUM calllength(s)
------------------- ------------------- ---------- -------------
2009-06-01 00:00:00 2009-06-01 00:30:002200
2009-06-01 00:30:00 2009-06-01 01:00:0000
2009-06-01 01:00:00 2009-06-01 01:30:001250
2009-06-01 01:30:00 2009-06-01 02:00:0000
2009-06-01 02:00:00 2009-06-01 02:30:0000
2009-06-01 02:30:00 2009-06-01 03:00:0000
2009-06-01 03:00:00 2009-06-01 03:30:0000
2009-06-01 03:30:00 2009-06-01 04:00:0000
2009-06-01 04:00:00 2009-06-01 04:30:0000
2009-06-01 04:30:00 2009-06-01 05:00:0000
2009-06-01 05:00:00 2009-06-01 05:30:0000

SEC_BEGINSEC_ENDCALLNUM calllength(s)
------------------- ------------------- ---------- -------------
2009-06-01 05:30:00 2009-06-01 06:00:0000
2009-06-01 06:00:00 2009-06-01 06:30:0000
2009-06-01 06:30:00 2009-06-01 07:00:0000
2009-06-01 07:00:00 2009-06-01 07:30:0000
2009-06-01 07:30:00 2009-06-01 08:00:0000
2009-06-01 08:00:00 2009-06-01 08:30:0000
2009-06-01 08:30:00 2009-06-01 09:00:0000
2009-06-01 09:00:00 2009-06-01 09:30:0000
2009-06-01 09:30:00 2009-06-01 10:00:0000
2009-06-01 10:00:00 2009-06-01 10:30:0000
2009-06-01 10:30:00 2009-06-01 11:00:0000

SEC_BEGINSEC_ENDCALLNUM calllength(s)
------------------- ------------------- ---------- -------------
2009-06-01 11:00:00 2009-06-01 11:30:0000
2009-06-01 11:30:00 2009-06-01 12:00:0000
2009-06-01 12:00:00 2009-06-01 12:30:0000
2009-06-01 12:30:00 2009-06-01 13:00:0000
2009-06-01 13:00:00 2009-06-01 13:30:0000
2009-06-01 13:30:00 2009-06-01 14:00:0000
2009-06-01 14:00:00 2009-06-01 14:30:0000
2009-06-01 14:30:00 2009-06-01 15:00:0000
2009-06-01 15:00:00 2009-06-01 15:30:0000
2009-06-01 15:30:00 2009-06-01 16:00:0000
2009-06-01 16:00:00 2009-06-01 16:30:0000

SEC_BEGINSEC_ENDCALLNUM calllength(s)
------------------- ------------------- ---------- -------------
2009-06-01 16:30:00 2009-06-01 17:00:0000
2009-06-01 17:00:00 2009-06-01 17:30:0000
2009-06-01 17:30:00 2009-06-01 18:00:0000
2009-06-01 18:00:00 2009-06-01 18:30:0000
2009-06-01 18:30:00 2009-06-01 19:00:0000
2009-06-01 19:00:00 2009-06-01 19:30:0000
2009-06-01 19:30:00 2009-06-01 20:00:0000
2009-06-01 20:00:00 2009-06-01 20:30:0000
2009-06-01 20:30:00 2009-06-01 21:00:0000
2009-06-01 21:00:00 2009-06-01 21:30:0000
2009-06-01 21:30:00 2009-06-01 22:00:0000

SEC_BEGINSEC_ENDCALLNUM calllength(s)
------------------- ------------------- ---------- -------------
2009-06-01 22:00:00 2009-06-01 22:30:0000
2009-06-01 22:30:00 2009-06-01 23:00:0000
2009-06-01 23:00:00 2009-06-01 23:30:0000
2009-06-01 23:30:00 2009-06-02 00:00:0000

已选择48行。


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15203236/viewspace-605346/,如需转载,请注明出处,否则将追究法律责任。
【SQL查询实现时间段区间统计】转载于:http://blog.itpub.net/15203236/viewspace-605346/

    推荐阅读