2017-08-02 43 views
1

我真的很困惑我如何得到以下結果與我有以下查詢。 當(LR.DATE_TO - LR.DATE_FROM)計算爲0我想要把0.5作爲結果,如果它是空的,我想把0作爲結果,否則我想使用從(LR.DATE_TO - LR.DATE_FROM)返回的確切區別。選擇oracle與如果檢查結果是0並返回0.5

請幫助

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; 
+1

使用'CASE'聲明。 'case LR.DATE_TO - LR.DATE_FROM = 0 then 0.5 else LR.DATE_TO - LR.DATE_FROM' – XING

回答

1

用例:

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

輝煌的答案謝謝! – danielad

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, . . .