oracle 时间分段查询

本文介绍了一种使用Oracle SQL进行时间段查询的方法,包括创建表、设置数据库时间格式以及如何进行全天分段查询的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*建表*/
create table test1(serviceid number,
callerno number,
begintime date,
endtime date
);




/*设置数据库时间格式*/
alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';






/*某个时段查询*/
select to_date('2009-06-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') +  (n - 1) / 48 sec_begin,
       to_date('2009-06-01 00:00:00', 'yyyy-mm-ddhh24:mi:ss') + n / 48 sec_end,
       count(callerno) callnum, 
       nvl(sum(endtime - begintime),0) * 24 * 60 * 60 "calllength(s)"
       
from test1 t, (select rownum n from dual connect by level < = 48) integers


where t.begintime 
between
      to_date('2009-06-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') + (n - 1) / 48  
      and
      to_date('2009-06-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') + n / 48
      
group by n;



/*全天分段查询*/
select to_date('2009-06-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') + (n - 1) / 48 sec_begin,
        to_date('2009-06-01 00:00:00', 'yyyy-mm-ddhh24:mi:ss') + n / 48 sec_end,
        count(callerno) callnum,
        nvl(sum(endtime - begintime),0) * 24 * 60 * 60 "calllength(s)"
        
from test1 t 
  
  right join (select rownum n from dual connect by level < = 48) integers
  on t.begintime 
  between
        to_date('2009-06-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') + (n - 1) / 48 
        and
        to_date('2009-06-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') + n / 48
        
group by n;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值