星期几选择案例的语法是什么?

时间:2017-02-16 16:34:25

标签: sql oracle oracle10g

如果当天是星期一(或者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 个答案:

答案 0 :(得分: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