MS Access使用Access sql - Null函数

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

在访问中使用Null函数。

我有四种不同的报告。所有都有订单号。在报表A中填写订单编号,其中订单编号在报表B中匹配,C&D在每个报表的单独列中。因此对于例如报告B中的订单号在报告A中匹配,然后填写订单号,否则没有(我不知道这是否是空白或Null,它没有匹配的订单号)好的,所以现在填写三个中的每一个报告A中的列。我想添加一个查询,通过使用Null函数查看报告A中的每一列,并在任何列中找到订单号,然后如果所有列都为“Null / Blank”则显示“Either” “然后说'无'。

我不是编码员。所以,如果您回复然后请求请通过更改报告名称给我一个确切的SQL我可以使用。

SELECT 
    [04_Match to Pending Consolidation].*,
    IIf(IsNull([Matched_Closed_Not_Invoiced_Report],0) + IsNull([Matched_Shipped_Not_Invoiced_report],0)+IsNull([Matched_Pending_Consolidation_Report],0),"NONE","EITHER REPORT") AS Validation 
FROM 
    [04_Match to Pending Consolidation];

我希望通过此查询在第四列中显示“Either”或“None”。

ms-access-2016
2个回答
0
投票

我猜它可以简化为:

SELECT 
    *,
    IIf(([Matched_Closed_Not_Invoiced_Report] + [Matched_Shipped_Not_Invoiced_report] + [Matched_Pending_Consolidation_Report]) Is Null, "NONE", "EITHER") AS Validation 
FROM 
    [04_Match to Pending Consolidation];

0
投票

可以通过多种方式编写表达式以实现相同的结果。与当前代码最相似的方法可能是使用Nz函数在给定表达式为null时返回0,例如:

SELECT 
    t.*,
    IIf
    (
        (
            Nz(t.[Matched_Closed_Not_Invoiced_Report], 0) + 
            Nz(t.[Matched_Shipped_Not_Invoiced_report], 0) + 
            Nz(t.[Matched_Pending_Consolidation_Report], 0)
        ) = 0,
        "NONE",
        "EITHER REPORT"
    ) AS Validation 
FROM 
    [04_Match to Pending Consolidation] t;

这当然假设所讨论的每个字段在非空时都保持数值。


另一种可能的方法是使用Is Null运算符的布尔逻辑,例如:

SELECT 
    t.*,
    IIf
    (
        t.[Matched_Closed_Not_Invoiced_Report] Is Null And
        t.[Matched_Shipped_Not_Invoiced_report] Is Null And
        t.[Matched_Pending_Consolidation_Report] Is Null,
        "NONE",
        "EITHER REPORT"
    ) AS Validation 
FROM 
    [04_Match to Pending Consolidation] t;
© www.soinside.com 2019 - 2024. All rights reserved.