访问SQL IIF语句无法正确评估

时间:2019-06-06 20:15:21

标签: sql ms-access

我有一个使用=IF(Z8:AI8 = "ED",Z7:AI7, " ")语句的Access SQL查询,该语句评估不正确。我看不到iif语句有问题。

下面的以下SQL代码产生下面的表。

iif

结果:

SELECT PR, 
    G21_Start AS Genesis_Date, 
    SWITCH(G21_comp IS NULL, 2.1,
               G22_comp IS NULL, 2.2,
               G31_comp IS NULL, 3.1,
               G32_comp IS NULL, 3.2,
               G33_comp IS NULL, 3.3,
               G4_comp IS NULL, 4,
               1=1, 0) AS Current_Gate, 
    DSUM ("Goal", "qry_Gate_Status", "Gate <= " & Current_Gate & " AND PR 
    = '" & PR & "'") AS Target_Days, 
   (DATE() - G21_Start) AS Elapsed, 
   IIF (Elapsed > Target_Days, "Overdue", "On Track") AS Contract_Health
FROM qry_Gate_Status_Extended;

PR Genesis_Date Current_Gate Target_Days Elapsed Contract_Health AA 3/8/2019 2.1 45 90 Overdue AB 1/1/2019 3.1 73 156 On Track BB 5/1/2019 2.2 59 36 On Track BC 9/19/2018 2.1 45 260 On Track 大于Elapsed时,Target_Days应该是Contract_Health,否则应该是"Overdue"

如您所见,结果不一致。我确实注意到的一件事是"On Track"Target_Days的结果)像文本字段一样左对齐,而不是数字右对齐。我一定在俯视

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

我使用VAL()函数将我认为是数字的数字转换为实际数字,并且可以正常工作。

VAL(DSUM ("Goal", "qry_Gate_Status", "Gate <= " & Current_Gate & " AND PR 
= '" & PR & "'")) AS Target_Days

感谢小费!!!你们摇滚!!!

答案 1 :(得分:1)

在比较之前,请尝试使用此表达式将文本(此处为字段Target_Days的值)转换为数字:

IIF ( Elapsed > VAL(Target_Days), "Overdue", "On Track")

有关 VAL 功能的详细信息,请参见: