0

Based on: How to check any missing number from a series of numbers?

I've got a similiar question. My source table has a sequence from 1 to 1000. But it is only bad if the gap is >1 and <20. I can't get the CONNECT BY to work.

Please help me.

SELECT 
    'XX' AS NETWORK
    ,'YY' AS TYPE
    ,min_seq - 1 + level AS MISSING
FROM ( 
    select 
        min(s.SEQUENCE_NUMBER) min_seq
        , max(s.SEQUENCE_NUMBER) max_seq
    FROM source s
)
CONNECT BY level <= max_seq - min_seq +20 AND level >= max_seq - min_seq +1
MINUS 
SELECT 
    'XX' AS NETWORK
    ,'YY' AS TYPE
    ,s.SEQUENCE_NUMBER AS EXISTING
FROM source s 
CasualBen
  • 829
  • 8
  • 22

2 Answers2

0

Use a CTE (with statement):

with CTE as
(
select level as NN
from dual
connect by level <= 20
)

select CTE.NN
from CTE 
left join source s
  on CTE.NN = s.SEQUENCE_NUMBER
where s.SEQUENCE_NUMBER is null
JohnHC
  • 10,935
  • 1
  • 24
  • 40
0

Old school connect by version

with tn as(
    -- sample data
    Select 1 n from dual
    union all
    Select 4  from dual
    union all
    Select 26  from dual
    union all
    Select 30  from dual
    union all
    Select 52  from dual
)
select distinct n, delta, n+level nn
from (
    select n, delta
    from (
        select n, lead(n) Over(order by n) - n delta
        from tn) t
    where delta between 2 and 20
    ) t2
connect by level < delta 
order by n
Serg
  • 22,285
  • 5
  • 21
  • 48