with ns as ( select 1 as n union select 2 as n union sele...

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for programmers

with ns as (
select 1 as n union
select 2 as n union
select 3 as n union
select 8 as n union
select 9 as n )
select * 
into #ns
from ns;

with 
inis as
(
   select n1.n, 'ini' as t 
   from #ns n1
   left outer join #ns n2 
       on n1.n = n2.n + 1
   where n2.n is null
   ),
fis as
(
   select n1.n , 'fi' as t
   from #ns n1
   left outer join #ns n2 
       on n1.n = n2.n - 1
   where n2.n is null
   )  
select inis.n as ini, min( fis.n ) as fi
from inis 
inner join fis 
  on inis.n <= fis.n
group by inis.n
;

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results
:records returned in :time ms:cached