2017-02-16 39 views
0

我試圖使用一個案例選擇如果一天是星期一(或d等於2),'其他'將處理每週的其他一天。什麼是星期選擇案例的語法?

Select to_char (sysdate, 'D') d from dual, 
/*if 2 or Monday then run this part*/ 
     CASE 
     2 
      Then 
      SELECT DISTINCT RPAD(CLAIM.CLAIM_NUMBER, 20) || 
        LPAD(CLAIM.CLAIM_ID, 15, '0') || 
        TO_CHAR(CLAIM.INCIDENT_DATE, 'MMDDYYYY') || 
        LPAD(' ',2) || 
        'D' 
       FROM CLAIM,CLAIMANT,INSURER,POLICY 
      WHERE CLAIM.CLAIM_ID = CLAIMANT.CLAIM_ID 
       AND POLICY.INSURER_NUMBER = INSURER.INSURER_NUMBER 
       AND INSURER.INSURER_NUMBER NOT IN ('7','8') 
       AND CLAIMANT.EXAMINER1_CODE NOT IN ('MAYCONV') 
       AND CLAIM.INSURANCE_TYPE = 2 
       AND CLAIM.JURISDICTION_CODE <> 1 
       AND CLAIMANT.CLAIMANT_TYPE_CODE in (4,7) 
       /*if Monday then go 2 days back*/ 
       AND CLAIMANT.EDIT_DATE > (SYSDATE - 2) 

/*if not equal to 2 or any day but Monday then run this part*/    
       Else 
       SELECT DISTINCT RPAD(CLAIM.CLAIM_NUMBER, 20) || 
        LPAD(CLAIM.CLAIM_ID, 15, '0') || 
        TO_CHAR(CLAIM.INCIDENT_DATE, 'MMDDYYYY') || 
        LPAD(' ',2) || 
        'D' 
       FROM CLAIM,CLAIMANT,INSURER,POLICY 
      WHERE CLAIM.CLAIM_ID = CLAIMANT.CLAIM_ID 
       AND POLICY.INSURER_NUMBER = INSURER.INSURER_NUMBER 
       AND INSURER.INSURER_NUMBER NOT IN ('7','8') 
       AND CLAIMANT.EXAMINER1_CODE NOT IN ('MAYCONV') 
       AND CLAIM.INSURANCE_TYPE = 2 
       AND CLAIM.JURISDICTION_CODE <> 1 
       AND CLAIMANT.CLAIMANT_TYPE_CODE in (4,7) 
       /*if not Monday then go 1 day back*/ 
       AND CLAIMANT.EDIT_DATE > (SYSDATE - 1) 
/
+1

側面問題,但你應該知道[NLS設置影響日數](http://stackoverflow.com/a/14518463/266304);其他人以不同的語言環境運行您的代碼可能會看到不同的結果。 –

回答

1

我沒有按照你的代碼緊密,但似乎你只是想在你的where子句中的最後一個條件依賴於一週中的一天。

如果是這樣,你需要類似的東西(警告:我沒有檢查代碼中的其他東西,以確保它是正確的或有效的!) - 唯一的變化是在最後一行。您不需要select... from dual,您只需要編寫select語句一次。

SELECT DISTINCT RPAD(CLAIM.CLAIM_NUMBER, 20) || 
        LPAD(CLAIM.CLAIM_ID, 15, '0') || 
        TO_CHAR(CLAIM.INCIDENT_DATE, 'MMDDYYYY') || 
        LPAD(' ',2) || 
        'D' 
       FROM CLAIM,CLAIMANT,INSURER,POLICY 
      WHERE CLAIM.CLAIM_ID = CLAIMANT.CLAIM_ID 
       AND POLICY.INSURER_NUMBER = INSURER.INSURER_NUMBER 
       ............ 
AND CLAIMANT.EDIT_DATE > SYSDATE - case to_char(sysdate, 'd') when 2 then 2 else 1 end 
+0

非常感謝! – johnrm