出力に希望する形式が不明です。これを行う1つの方法があります。
alter session set nls_date_format = 'dd/mm/yyyy';
with
test_data (period_no, from_dt, to_dt) as (
select 9891, to_date('01/06/2016'), to_date('31/07/2016') from dual union all
select 9892, to_date('01/08/2016'), to_date('30/09/2016') from dual union all
select 9893, to_date('01/09/2016'), to_date('31/10/2016') from dual union all
select 9894, to_date('01/11/2016'), to_date('31/12/2016') from dual union all
select 9895, to_date('15/12/2016'), to_date('28/02/2017') from dual union all
select 9896, to_date('01/03/2017'), to_date('31/05/2017') from dual
)
-- End of simulated table (for testing purposes only, not part of the solution).
-- SQL query begins BELOW THIS LINE.
select a.period_no as period_a, a.from_dt as from_dt_a, a.to_dt as to_dt_a,
b.period_no as period_b, b.from_dt as from_dt_b, b.to_dt as to_dt_b
from test_data a
join
test_data b
on a.period_no < b.period_no
and a.to_dt >= b.from_dt
and b.to_dt >= a.from_dt
;
PERIOD_A FROM_DT_A TO_DT_A PERIOD_B FROM_DT_B TO_DT_B
---------- ---------- ---------- ---------- ---------- ----------
9892 01/08/2016 30/09/2016 9893 01/09/2016 31/10/2016
9894 01/11/2016 31/12/2016 9895 15/12/2016 28/02/2017
は、我々は同じテーブルではなく、別のテーブルに(異なる行を比較したいのですが、コンセプトは同じであるため、それ自体にテーブルを結合する - このような比較のために、あなたもそれならば、テーブルを結合します同じ表の2つのコピーです)。これを「自己結合」といいます。
次に、時間間隔が重ならない2つの方法があります。最初の1つは2番目のものが始まる前に終了し、2番目のものは最初のものが始まる前に終了します。今度はこの条件を否定します( "or"の否定は "and"です)、JOIN節に2つの余分な条件があります。
[私のブログの投稿](http://tonyandrews.blogspot.co.uk/search/label/overlap) –
「date」データ型のFROM_DATEとTO_DATEはありますか? (彼らはすべきです!) – mathguy
@TonyAndrews投稿したばかりのリンクを確認してください。それは私のために働いていません。 – mathguy