create table FillTable(ID,StaV,EndV,FillS,FillE) as
select 1,2, 5, 2, 3 from dual union all
select 1,2, 5, 4, 5 from dual union all
select 2,3, 6, 3, 4 from dual union all
select 2,3, 6, 4, 7 from dual union all
select 3,4, 7, 3, 4 from dual union all
select 3,4, 7, 4, 7 from dual union all
select 4,4, 8, 4, 5 from dual union all
select 4,4, 8, 7, 8 from dual union all
select 5,5,10, 5, 6 from dual union all
select 5,5,10, 7, 8 from dual union all
select 5,5,10, 9,10 from dual union all
select 6,5,11, 5, 6 from dual union all
select 6,5,11, 8, 9 from dual union all
select 6,5,11,10,11 from dual union all
select 7,6,10, 6,10 from dual union all
select 8,8,13, 6,13 from dual;
--■■■階層問い合わせを使う方法(10g以降)■■■
select ID,StaV,EndV from FillTable
minus
select ID,StaV,EndV
from (select ID,StaV,EndV,FillS,FillE,
min(FillS) over(partition by ID) as MinFillS,
count(*) over(partition by ID) as cnt
from FillTable)
where connect_by_IsLeaf = 1
and EndV = FillE
and Level = cnt
start with FillS = all(StaV,MinFillS)
connect by nocycle prior ID = ID
and prior FillE+1 = FillS;
--■■■再帰with句を使う方法(11gR2以降)■■■
with rec(ID,StaV,EndV,FillS,FillE,Val) as(
select ID,StaV,EndV,FillS,FillE,FillS
from FillTable
union all
select ID,StaV,EndV,FillS,FillE,Val+1
from rec
where Val+1 <= FillE)
select ID,StaV,EndV
from rec
group by ID,StaV,EndV
having not(EndV-StaV+1
=all(count(*),
count(distinct case when Val between StaV and EndV
then Val end)))
order by ID;