我真的很困惑,我可以通过以下查询得到以下结果。
当(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;
答案 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, . . .