您可以利用
lag
窗口函数和内部查询,如下所示:
-- input data
with your_table as(
select 7256 a, 1 b, 1 c, 256 d from dual union all
select 14632, 2, 2, 1052 from dual union all
select 22428, 3, 3, 1428 from dual union all
select 29864, 4, 4, 1864 from dual union all
select 36472, 5, 5, 8864 from dual union all
select 44699, 6, 6, 15472 from dual union all
select 51467, 7, 7, 16840 from dual union all
select 53067, 8, 8, 23840 from dual union all
select 60371, 9, 9, 25067 from dual union all
select 66171, 10, 10, 32067 from dual
)
-- Actual query
select t.a, t.b, t.c, tt.d, t.a-tt.d as diff from
(select a,b,c,d, COALESCE(lag(a) over (order by a), 0) lag_a
from your_table t) t join
(select a,b,c,d, COALESCE(lag(d) over (order by d), 0) lag_d
from your_table t) tt
on t.a > tt.lag_d and TT.D > T.LAG_A
order by t.a, tt.d
输出:
A B C D DIFF
---------- ---------- ---------- ---------- ----------
7256 1 1 256 7000
7256 1 1 1052 6204
7256 1 1 1428 5828
7256 1 1 1864 5392
7256 1 1 8864 -1608
14632 2 2 8864 5768
14632 2 2 15472 -840
22428 3 3 15472 6956
22428 3 3 16840 5588
22428 3 3 23840 -1412
29864 4 4 23840 6024
29864 4 4 25067 4797
29864 4 4 32067 -2203
36472 5 5 32067 4405
14 rows selected.