connect_by

create table wzh_test_date(begin_date date,end_date date)
truncate table wzh_test_date
insert into wzh_test_date values(trunc(sysdate,'dd'),trunc(sysdate,'dd')+1);
insert into wzh_test_date values(trunc(sysdate,'dd')+1,trunc(sysdate,'dd')+2-1/24);

insert into wzh_test_date values(trunc(sysdate,'dd')+2,trunc(sysdate,'dd')+3-1/12);

insert into wzh_test_date values(trunc(sysdate,'dd')+3,trunc(sysdate,'dd')+4);

insert into wzh_test_date values(trunc(sysdate,'dd')+4,trunc(sysdate,'dd')+5+1/24);

insert into wzh_test_date values(trunc(sysdate,'dd')+5,trunc(sysdate,'dd')+6+2/24);
insert into wzh_test_date values(trunc(sysdate,'dd')+6,trunc(sysdate,'dd')+7);

with rs
as (select begin_date,end_date,row_number()over(order by begin_date)row_no,row_number()over(order by begin_date)-1 row_no_desc
from wzh_test_date)
select mt1.begin_date,mt2.end_date
from
(select l.begin_date,l.end_date,row_number()over(order by l.begin_date)row_no
 from   rs l
left join
        rs r
 on  l.row_no-1=r.row_no
 and l.begin_date=r.end_date)mt1
 join      
( select r.begin_date,r.end_date,row_number()over(order by r.begin_date)row_no
 from   rs l
 right join
        rs r
 on  l.row_no-1=r.row_no
 and l.begin_date=r.end_date)mt2
 on mt1.row_no=mt2.row_no
 order by mt1.begin_date
 
 
 with rs
as (select begin_date,end_date,row_number()over(order by begin_date)row_no,row_number()over(order by begin_date)+1 row_no_desc
from wzh_test_date)
select connect_by_root begin_date,rs.*,level,CONNECT_BY_ISLEAF isleaf
from rs
connect by nocycle (prior end_date=begin_date)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值