The task is the following: I have a table "numbers" which has only one column "number". There are numbers in this table in range a...b, each specific number appears in table only once. I need to find missing numbers in range a..b. One more thing is that range can be very big, and performance should be still good. I came up with idea to find all number pairs, between which numbers are missing, so we don't have to compare all numbers in this range and improve performance this way. The next query returns all such number pairs:
select N1.number as num1, MIN(N2.number)as num2 from numbers N1 join numbers N2 on N2.number > N1.number
and N2.number - N1.number > 1
and not exists (select * from numbers where number > N1.number and number < n2.number)
group by n2.number, n1.number
The result for set 1 2 3 4 6 8 10 is:
4 6
6 8
8 10
Now I want to list all numbers between, but I stucked, I'm failing to pass these number pairs in other select statement or whatever and make it one query. Any ideas? is it possible at all?