我需要在SQL中重写以下IF-THEN代码,以下是我的第一次尝试。但是当我运行SQL代码时,它返回了一条错误消息:ORA-00933:SQL命令未正确结束。有人可以帮忙吗?
而且,我想知道除了使用嵌套的Case语句之外是否有更好的替代方法来重写SAS代码。我是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
答案 0 :(得分: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
(如果这是原始值,则可能没有区别。)