How to find gaps in a sequence of numbers in SQL Server
问题 I am trying to find the smallest missing number in this table. +------+--------+ | id | number | +------+--------+ | 902 | 1 | | 908 | 2 | | 1007 | 7 | | 1189 | 8 | | 1233 | 12 | | 1757 | 15 | +------+--------+ In the number column, you can see there are several gaps between the numbers. I need to get the number after the smallest gap. So in the case above I need the number 3. Because 2 is the smallest number after a gap. 回答1: I would use lead() : select min(id) + 1 from (select t.*, lead(id)