在 Access Table 中查找丢失的序列号

Find Missing Sequence Numbers in Access Table

我在 Access 2003 数据库中有一个名为 Comments 的 MS Access table,在 Comments table 中有一个名为 Sequence Number 的列。 Sequence Number 列的数字范围从 1 到 20000。但是,Sequence Number 列中缺少某些数字,我希望能够查看缺少的数字,例如下面我想 运行 访问查询以查看缺少 4。

Sequence Number
 1
 2
 3
 5
 6

我在 SQL 视图中使用以下访问查询来获得我想要的内容。

SELECT ([Sequence Number]+1) AS MissingFrom, DMin("Sequence Number","Comments","Sequence Number>" & [Sequence Number]) AS MissingUntil
FROM Comments
WHERE (((DMin("Sequence Number","Comments","Sequence Number>" & [Sequence Number]))<>([Sequence Number]+1)));

但是,当我 运行 查询时,出现以下错误:

Syntax error (missing operator) in query expression 'Min(Sequence Number)'. 

有人可以指出导致查询失败的原因吗?谢谢!

您可以使用以下方法获得缺失系列中的第一个:

select num + 1
from comments
where num + 1 not in (select num from comments) and
      num + 1 <> (select max(num) from comments);

NOT EXISTS:

SELECT MIN([Sequence Number]) + 1 
FROM Comments AS c
WHERE
  c.[Sequence Number] < (SELECT MAX([Sequence Number]) FROM Comments)
  AND NOT EXISTS (
    SELECT 1 FROM Comments
    WHERE [Sequence Number] = c.[Sequence Number] + 1
  )