2014-08-27 91 views
0

我需要在SQL中重寫以下IF-THEN代碼,下面是我的第一次嘗試。但是當我運行SQL代碼時,它返回了一條錯誤消息:ORA-00933:SQL命令未正確結束。任何人都可以請幫忙?需要關於嵌套CASE語句的幫助

此外,我想知道是否有更好的替代方法來重寫SAS代碼,而不是使用嵌套的Case語句。我是SQL的新手。任何幫助將不勝感激!

/********************** SAS代碼****************** *****/

if Record_type = 'Cliam'and Claim_Type_E0141 in ('C','H','M','F','I','N','K','Z') THEN DO; 
if POS in ('21','22','23','24','25','26','27','28') then POS_new=31; 
if POS in ('86') then POS_new=32; 
if POS in ('81','82') then POS_new=34; 
('61','62','63','64','65','66','67','68') then POS_new=54; 
END; 

if Record_type = 'Encounter'and ENCOUNTER_TYPE_H054 in ('I') THEN DO; 
if BILL_TYPE_CODE_1_2 in ('21','22','23','24','25','26','27','28') then POS_new=31; 
if BILL_TYPE_CODE_1_2 in ('86') then POS_new=32; 
if BILL_TYPE_CODE_1_2 in ('81','82') then POS_new=34; 
if BILL_TYPE_CODE_1_2 in ('61','62','63','64','65','66','67','68') then POS_new=54; 
END; 

/*********************** SQL代碼******* ****************/

UPDATE ALL_INPT 
SET POS_new = 
    WHEN Record_type = "Claim" and Claim_Type_E0141 in ('C','H','M','F','I','N','K','Z') 
      THEN CASE 
       WHEN POS in ('21','22','23','24','25','26','27','28') 
         THEN '31' 
       WHEN POS in ('86') 
         THEN '32' 
       WHEN POS in ('81','82') 
         THEN '34' 
       WHEN POS in ('61','62','63','64','65','66','67','68') 
         THEN '54' 
       ELSE POS 
      END 

    WHEN Record_type = "Encounter" and ENCOUNTER_TYPE_H054in ('I') 
      THEN CASE 
       WHEN BILL_TYPE_1_2_E0394 in ('21','22','23','24','25','26','27','28') 
         THEN '31' 
       WHEN BILL_TYPE_1_2_E0394 in ('86') 
         THEN '32' 
       WHEN BILL_TYPE_1_2_E0394 in ('81','82') 
         THEN '34' 
       WHEN BILL_TYPE_1_2_E0394 in ('61','62','63','64','65','66','67','68') 
         THEN '54' 
       ELSE BILL_TYPE_1_2_E0394 
      END     
END 

;

+4

不知道任何SAS,但它看起來像你的SQL缺少外部CASE。應該是「SET POS_new =」和「WHEN」之間的CASE。 – 2014-08-27 17:54:41

回答

1

內部case在兩種情況下都是相同的。我會將其簡化爲:

UPDATE ALL_INPT 
    SET POS_new = (CASE WHEN Record_type = 'Claim' and Claim_Type_E0141 in ('C','H','M','F','I','N','K','Z') OR 
          Record_type = 'Encounter' and ENCOUNTER_TYPE_H054 in ('I') 
         THEN (CASE WHEN POS in ('21','22','23','24','25','26','27','28') 
            THEN '31' 
            WHEN POS in ('86') 
            THEN '32' 
            WHEN POS in ('81','82') 
            THEN '34' 
            WHEN POS in ('61','62','63','64','65','66','67','68') 
            THEN '54' 
            WHEN Record_type = 'Claim' 
            ELSE BILL_TYPE_1_2_E0394 
           END) 
         ELSE pos_new     
        END); 

SAS代碼中沒有else子句。但是,如果沒有條件滿足,則pos_new的值不變。您的版本將值設置爲NULL(如果這是原始值,則可能沒有區別)。

+0

非常感謝您的幫助!我只是想出了一種可行的方式。但是你的版本更加簡潔。真的很感激:D – Christy 2014-08-28 14:40:23