17,377
社区成员
发帖
与我相关
我的任务
分享
select max(tab2.name)
from (select rownum a, D_BEGIN_TIME c, D_END_TIME d from table_test) tab1,
(select rownum a, D_BEGIN_TIME c, D_END_TIME d from table_test) tab2
where tab2.D_BEGIN_TIME - tab1.D_END_TIME = 10
and tab2.rownum - tab1.rownum = 1;
scott@ORA1> select * from t;
NAME D_BEGIN_TIME D_END_TIME
-------------------- ------------------- -------------------
n1 2008-01-02 00:00:00 2008-01-04 00:00:00
n2 2008-01-14 00:00:00 2008-03-05 00:00:00
n3 2008-03-15 00:00:00 2008-03-22 00:00:00
n4 2008-03-25 00:00:00 2008-04-01 00:00:00
scott@ORA1>
scott@ORA1> with x as (
2 select name
3 ,d_begin_time
4 ,d_end_time
5 ,d_begin_time - lag(d_end_time) over(order by rownum) gaps
6 from t
7 )
8 select *
9 from (
10 select name
11 ,d_begin_time
12 ,d_end_time
13 ,row_number() over(order by d_begin_time desc) rn
14 from x
15 where gaps = 10
16 )
17 where rn = 1;
NAME D_BEGIN_TIME D_END_TIME RN
-------------------- ------------------- ------------------- ----------
n3 2008-03-15 00:00:00 2008-03-22 00:00:00 1