首页 > 解决方案 > 具有多个语句的 SQL 案例查询

问题描述

我正在尝试使用案例查询创建一个新列,但我也想在我的其他案例查询中使用它,如下所示,我无法执行下面的查询。请发表你的意见。谢谢

    select

    case  when ccis.id  is not null then 'High'
    else null 
    end as category,
    
    case  
    when category is not NULL then True
    else False 
    end as flag



    FROM "view1" as ccis left outer join "view2" as cctm 
    on ccis.study_name = cctm.study_name 


    from goes here ---
    Join  statement goes here ---

标签: sqloracle

解决方案


您不能category在下一个CASE表达式中使用。
但是您可以应用与前面相同的逻辑CASE

select
  case when id  is not null then 'High' end as category,
  case when id  is not null then 'True' else 'False' end as flag
  ..........................

因为category不是nullid不是null
else null从第一个CASE表达式中删除,因为它是不需要的,因为它是在不满足分支null中的任何条件时返回的默认功能。when


推荐阅读