【问题标题】:Find Missing Sequence Numbers in Access Table在访问表中查找缺失的序列号
【发布时间】:2020-05-26 05:04:08
【问题描述】:

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

Sequence Number
 1
 2
 3
 5
 6

我在 SQL 视图中使用以下 Access 查询来获得我想要的。

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)'. 

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

【问题讨论】:

  • 字段 Sequence Number 有一个空格,因此必须包含在 [ ] 中,即使在域聚合函数参数中也是如此。然而,奇怪的是,带有空格的表或查询名称在域聚合函数参数中不需要 []。使用标点符号/特殊字符还需要 [ ] 来分隔对象名称。此外,保留字作为名称可能会导致问题。建议在命名约定中避免使用这些功能。

标签: sql ms-access ms-access-2003


【解决方案1】:

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
  )

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      • 2021-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多