Ismael L. Donis Garcia wrote:

Is it possible to query for the first number that does not exist in a sequence of numbers?

For Example:

Table.Id
1
2
4
5
8
9
....

select a.id from table a where a.id between 1 and 1000000

The intended result should be 3

Ivan Prenosil answers:

SELECT FIRST 1 ID+1
  FROM (SELECT * FROM MyTable WHERE ID BETWEEN 1 AND 1000000
        UNION SELECT 0 FROM RDB$DATABASE)
  WHERE ID+1 NOT IN (SELECT * FROM MyTable)
  ORDER BY ID;

Like this post? Share on: TwitterFacebookEmail


Related Articles


Author

Firebird Community

Published

Category

Gems from Firebird Support list

Tags