Wednesday, July 28, 2010

SQL to find non-consecutive numbers in a column

We had a need to find non-consecutive numbers in a sequence of numbers. This snippet of SQL finds userIDs which are non-consecutive.
SELECT A.userID + 1
FROM MyTable AS A
WHERE NOT EXISTS (
    SELECT B.userID FROM MyTable AS B 
    WHERE A.userID + 1 = B.userID)
GROUP BY A.userID;

1 comment:

Anonymous said...

Excellent!