2017-08-04 77 views
2

您好我正嘗試在Oracle SQL Developer中創建一個視圖。 我希望視圖是raw_test與新列的所有內容作爲「排除原因」,排除原因值分別爲「Patient_ID_Missing」和「Duplicate_MRN」。Oracle With,Case When Statement missing關鍵字

With 
Dup_MRN AS 
    (SELECT * 
FROM raw_test 
    WHERE mrn IN ( SELECT mrn 
       FROM raw_test 
      GROUP BY mrn 
       HAVING COUNT (*) > 1)) 
Select raw_test.*, 
    case when raw_test.patient_ID_CDW is null then 'Patient_ID_Missing' 
    case when Dup_MRN.mrn is not null then 'Duplicate_MRN' 
    End as "Exclusion_reason" 
From raw_test    
Left join dup_mrn.mrn on raw_test.mrn = dup_mrn.mrn 

當我運行這個我得到的錯誤「缺少關鍵字」,但我無法弄清楚我到底錯過了什麼。

在此先感謝您的幫助

*輝煌!謝謝大家的幫助,我絕對錯過了那裏的第二個案例條款。我認爲這是簡單的東西,你可以看看三葉草場幾個小時,你看到一個四個葉前:)

+3

你有一個額外的'case',它的'情況下,當「東西」,然後「東西」的時候......' – Lamak

+0

如果下面的一個答案解決您的問題,請把它標記爲接受的答案。 – SandPiper

+0

如果該消息表示「EXTRA KEYWORD」而不是「MISSING KEYWORD」,則該消息會更清晰。我想他們希望你在第二次(但這裏不需要)CASE之前放入'END'。 – Hogan

回答

0

您正在使用的case statement不正確。這應該有

case 
    when exp1 then thing1 
    when exp2 then thing2 
    else default_thing 
end as field_name 

with up_mrn as (
    select 
    * 
    from 
    raw_test 
    where 
    mrn in (
     select 
     mrn 
     from 
     raw_test 
     group by 
     mrn 
     having count (*) > 1 
    ) 
) 
select 
    raw_test.* 
    , case 
    when raw_test.patient_id_cdw is null 
     then 'Patient_ID_Missing' 
    when dup_mrn.mrn is not null 
     then 'Duplicate_MRN' 
    else null 
    end as "Exclusion_reason" 
from 
    raw_test    
    left join dup_mrn.mrn 
    on raw_test.mrn = dup_mrn.mrn 
0

您正在使用CASE語句不正確的格式。

SELECT raw_test.*, 
    CASE 
    WHEN raw_test.patient_ID_CDW IS NULL THEN 'Patient_ID_Missing' 
    WHEN Dup_MRN.mrn IS NOT NULL THEN 'Duplicate_MRN' 
    ELSE '' 
    END AS "Exclusion_reason"