【问题标题】:Access SQL query with nulls and aggregate function使用空值和聚合函数访问 SQL 查询
【发布时间】:2014-06-23 16:21:37
【问题描述】:

试图在 MS Access 中提出 SQL 查询,但空值和聚合函数让我难过。任何帮助表示赞赏。

查询以显示 TABLE1 中 EditDate(可能为空)大于 TABLE2 中的最大 LastImportDate 的记录。

表 1

字段名称 - 数据类型 ReportID - 编号 EditDate - 日期/时间

表2

字段名称 - 数据类型 LastImportDate - 日期/时间

谢谢。

【问题讨论】:

  • COALESCEISNULL 在 Access 的 SQL 中工作吗?如果EditDateNULL,你想忽略它吗?你可能会使用类似... where ISNULL(TABLE1.EditDate, '1970-01-01') > TABLE2.LastImportDate...
  • 这是link 或在 MS Access 中使用 ISNULL。像这样的东西:... where IIF(ISNULL(TABLE1.EditDate), '1970-01-01', TABLE1.EditDate) > TABLE2.LastImportDate...

标签: sql ms-access-2013


【解决方案1】:
SELECT *
FROM table1
WHERE editDate > (
    SELECT max(lastImportDate)
    FROM   table2
)

不确定这究竟是如何转化为访问查询的,但这就是想法。

此外,如果您可以将最大日期分解为一个单独的变量,这将使您获得更好的性能 - 类似于:

DECLARE @maxDate DATETIME
SET @maxDate = (
    SELECT max(lastImportDate)
    FROM   table2
)

SELECT *
FROM table1
WHERE editDate > @maxDate

最后,如果您想要编辑日期为空的日期,您可以让查询将空值解释为任意日期,例如 isNull(editDate, '1900-01-01') - 这将使空的 editDate 被解释为 1900 1 月 1 日

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 2018-12-09
    相关资源
    最近更新 更多