【问题标题】:Oracle With, Case When statement missing keywordOracle With, Case When 语句缺少关键字
【发布时间】:2018-01-12 15:14:50
【问题描述】:

您好,我正在尝试在 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

当我运行它时,我收到错误“缺少关键字”,但我无法弄清楚我到底缺少什么。

提前感谢您的帮助

*太棒了!谢谢大家的帮助,我肯定错过了那里的第二个案例条款。我认为这很简单,你可以在一片三叶草上看几个小时,然后才能看到一棵四叶草:)

【问题讨论】:

  • 你有一个额外的case,它是case when "something" then "something" when ...
  • 如果以下答案之一解决了您的问题,请将其标记为已接受的答案。
  • 如果该消息显示“额外关键字”而不是“缺少关键字”,则会更清楚。我猜他们希望你在第二个(但这里不需要)CASE 之前输入END

标签: sql oracle with-statement case-when


【解决方案1】:

您错误地使用了 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"

【讨论】:

    【解决方案2】:

    您错误地使用了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
    

    【讨论】:

      猜你喜欢
      • 2020-01-27
      • 2015-04-17
      • 1970-01-01
      • 2020-02-08
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多