Oracle With,Case When语句缺少关键字

时间:2017-08-04 15:48:17

标签: sql oracle with-statement case-when

您好我正在尝试在Oracle SQL Developer中创建一个视图。 我希望这个视图能够成为raw_test中的所有内容,其中包含新列As' Exclusion Reason'其中排除原因值为“Patient_ID_Missing'和' Duplicate_MRN。

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

当我运行此操作时,我收到错误"缺少关键字"但是我无法弄清楚我到底错过了什么。

提前感谢您的帮助

*辉煌!谢谢大家的帮助,我绝对错过了那里的第二个案例条款。我认为这很简单,你可以看到一个三叶草的领域,然后你会看到一个有四片叶子的三叶草:)

2 个答案:

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

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