如何在resul中排除负值

时间:2019-07-09 14:29:16

标签: sql ms-access

请问如何获取此查询以排除负(-)值

SELECT Enrollment.Participant_ID
    ,Enrollment.Site
    ,Enrollment.ActualDelivery
    ,Forms.[2MonthQoL]
    ,[Enrollment].[ActualDelivery] + 70 AS Due
    ,DateDiff("d", [Due], DATE ()) AS DaysOverdue
FROM Enrollment
LEFT JOIN Forms ON Enrollment.Participant_ID = Forms.Participant_IDFK
WHERE (
        ((Enrollment.ActualDelivery) IS NOT NULL)
        AND ((Forms.[2MonthQoL]) IS NULL)
        );

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以将条件添加到where子句中:

WHERE Enrollment.ActualDelivery) IS NOT NULL AND
      Forms.[2MonthQoL]) IS NULL AND
      DateDiff("d", [Due], DATE()) >= 0

这自然会写成:

WHERE Enrollment.ActualDelivery) IS NOT NULL AND
      Forms.[2MonthQoL]) IS NULL AND
      [Due] <= DATE()

答案 1 :(得分:0)

您可以使用函数ABS()来获取数字的绝对值,例如:

SELECT Enrollment.Participant_ID
    ,Enrollment.Site
    ,Enrollment.ActualDelivery
    ,Forms.[2MonthQoL]
    ,[Enrollment].[ActualDelivery] + 70 AS Due
    ,ABS(DateDiff("d", [Due], DATE ())) AS DaysOverdue
FROM Enrollment
LEFT JOIN Forms ON Enrollment.Participant_ID = Forms.Participant_IDFK
WHERE (
        ((Enrollment.ActualDelivery) IS NOT NULL)
        AND ((Forms.[2MonthQoL]) IS NULL)
        );

如果只希望这些负数为NULL,则可以使用IIF()

SELECT Enrollment.Participant_ID
    ,Enrollment.Site
    ,Enrollment.ActualDelivery
    ,Forms.[2MonthQoL]
    ,[Enrollment].[ActualDelivery] + 70 AS Due
    ,IIF((DateDiff("d", [Due], DATE ())<0, NULL, (DateDiff("d", [Due], DATE ())) AS DaysOverdue
FROM Enrollment
LEFT JOIN Forms ON Enrollment.Participant_ID = Forms.Participant_IDFK
WHERE (
        ((Enrollment.ActualDelivery) IS NOT NULL)
        AND ((Forms.[2MonthQoL]) IS NULL)
        );