oracle 时间交叉,关于Oracle范围交叉和重复的查询

SQL codewith userhistory as

(

select 25 userid,date'2012-04-01' starttime,date'2012-06-30' endtime from dual

union all

select 25 userid,date'2012-07-01' starttime,date'2012-08-30' endtime from dual

union all

select 25 userid,date'2012-07-01' starttime,date'9999-09-09' endtime from dual

union all

select 34 userid,date'2008-04-10' starttime,date'2008-06-30' endtime from dual

union all

select 34 userid,date'2008-07-01' starttime,date'2008-07-31' endtime from dual

union all

select 34 userid,date'2008-09-01' starttime,date'2008-09-30' endtime from dual

union all

select 34 userid,date'2008-10-01' starttime,date'2008-12-31' endtime from dual

union all

select 34 userid,date'2009-01-01' starttime,date'2009-06-30' endtime from dual

union all

select 34 userid,date'2009-07-01' starttime,date'2009-07-31' endtime from dual

union all

select 34 userid,date'2009-10-01' starttime,date'2009-12-31' endtime from dual

)

select t1.userid,t1.starttime,t1.endtime

from (select rownum rn,a.* from userhistory a) t1,

(

select rn,userid,starttime,endtime

from

(

select rownum rn,userid,starttime,endtime,lag(endtime) over(partition by userid order by rownum) t_time

from userhistory

)

where starttime-t_time<>1

) t2

where t1.rn=t2.rn or t1.rn+1 = t2.rn

order by t1.userid,t1.starttime

userid starttime endtime

----------------------------------------------

1 25 2012/7/1 2012/8/30

2 25 2012/7/1 9999/9/9

3 34 2008/7/1 2008/7/31

4 34 2008/9/1 2008/9/30

5 34 2009/7/1 2009/7/31

6 34 2009/10/1 2009/12/31

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值