在SQL Server中发现列空白

问题描述 投票:0回答:1

我有一个带有列,整数类型的表,它不是主键。我有数千条记录。

我想找到丢失的ID。

我有这些数据:

1
2
3
4
6
8
11
14

我想要得到这个结果:5,7,9,10,12,13

你知道我该怎么做吗?

谢谢,

sql sql-server
1个回答
0
投票

更容易将其作为范围:

select (col + 1) as first_missing, (next_col - 1) as last_missing
from (select t.*, lead(col) over (order by col) as next_col
      from t
     ) t
where next_col <> col + 1
© www.soinside.com 2019 - 2024. All rights reserved.