如果检查结果为0并返回0.5,请选择oracle

时间:2017-08-02 11:57:39

标签: sql oracle

我真的很困惑,我可以通过以下查询得到以下结果。 当(LR.DATE_TO - LR.DATE_FROM)计算为0时,我想将0.5作为结果,如果它为null,我想将0作为结果,否则我想使用从(LR.DATE_TO - LR.DATE_FROM)返回的确切差异。

请,帮助

    INSERT INTO LEAVE_TAKEN_COUNT (NUMDAYS,EMPLOYEE_SERIAL_ID)
    SELECT NVL((LR.DATE_TO - LR.DATE_FROM), 0) as NumDays , T.ID
    FROM TBL_EMPLOYEE T 
    LEFT JOIN TBL_LEAVE_REQUEST LR on T.ID = LR.EMPLOYEE_SERIAL_ID
    WHERE T.DATE_PERMANENT IS NOT NULL
    ORDER BY T.ID; 

2 个答案:

答案 0 :(得分:1)

使用CASE:

INSERT INTO LEAVE_TAKEN_COUNT (NUMDAYS,EMPLOYEE_SERIAL_ID)
    SELECT CASE WHEN LR.DATE_TO - LR.DATE_FROM = 0 THEN 0.5
                WHEN (LR.DATE_TO - LR.DATE_FROM) IS NULL THEN 0
                ELSE LR.DATE_TO - LR.DATE_FROM
            END as NumDays , T.ID
    FROM TBL_EMPLOYEE T 
    LEFT JOIN TBL_LEAVE_REQUEST LR on T.ID = LR.EMPLOYEE_SERIAL_ID
    WHERE T.DATE_PERMANENT IS NOT NULL
    ORDER BY T.ID; 

答案 1 :(得分:1)

使用case

SELECT (CASE WHEN LR.DATE_TO IS NULL OR LR.DATE_FROM IS NULL THEN 0
             WHEN LR.DATE_TO = LR.DATE_FROM THEN 0.5
             ELSE LR.DATE_TO - LR.DATE_FROM
        END) as NumDays, . . .