5

我在 Access DB 的 VB 应用程序中有这个查询:

  SELECT DISTINCT Specialization, MAX(a.faultZone) AS faultZone, ISNULL(a.faultCount, 0) AS NoOfFaults  FROM Technicians AS t 
    LEFT JOIN 
             ( 
            SELECT DISTINCT Faults.[Type] AS faultType, MAX(Faults.[Zone]) AS faultZone, COUNT(Faults.[Type]) AS faultCount 
            FROM Faults "
            WHERE Faults.[Zone] = 8 " ' this value will be from variable
            GROUP BY Faults.[Type] "
            ) AS a 
    ON (t.Specialization = a.faultType) 
    WHERE t.specialization <> 'None' "
    GROUP BY a.faultCount, t.Specialization 

它给出了以下我无法解决的问题......

“查询表达式'ISNULL(a.faultCount,0'中的函数使用的参数数量错误。”

我想要实现的只是将值设置NoOFFaults为零,这意味着特定区域没有故障。

谢谢你

4

3 回答 3

7

Just to add my two cents, and while I like the simple syntax of Nz(), if you seek trouble free performance, both IsNull() and NZ() should be avoided in favor of Is Null:
IIF(a.faultCount Is Null, 0, a.faultCount).

See the excellent explanation here: http://allenbrowne.com/QueryPerfIssue.html

Also, if your tables are in SQL Server or Oracle, using Nz() will force more of the query to be executed locally, with a HUGE performance impact.

于 2012-11-29T20:43:04.333 回答
5

Microsoft Access 的版本与IsNull大多数 SQL 版本不同;如果值为 NULL,它只返回 TRUE,如果不是,则返回 FALSE 。

您基本上需要使用以下方法构建自己的IIF()

IIF(ISNULL(a.faultCount), 0, a.faultCount)
于 2012-11-29T20:20:49.073 回答
5

我认为您正在寻找 nz 功能

Nz(a.faultCount, 0)

如果值为空,将返回 0

于 2012-11-29T20:27:04.667 回答