【问题标题】:Using switch to select columns and filter使用 switch 选择列和过滤器
【发布时间】:2016-06-22 14:43:12
【问题描述】:

我们可以这样使用switch case来过滤表的列吗?

select distinct stud.name, stud.num,
        case WHEN stud.sub like '%data%'            THEN    stud.sub
             WHEN stud.sub like '%informatics%'     THEN    stud.sub
             WHEN stud.sub like '%genomics%'        THEN    stud.sub
                                                    ELSE    '---'
        END
from  table_A

预期结果是

Name    ID      Sub

victor  2098    -----
Lui     6754    Bioinformatics
Willis  7653    Advanceddatascience

谢谢!

【问题讨论】:

  • postgresql ?甲骨文?
  • 你有什么错误吗?你试过了吗?
  • 答案是肯定的,您可以,而且我看不出您的查询有什么问题。
  • 没有错误,但结果只有一行。如果切换案例失败,我希望所有行都以“----”作为 stud.sub 列出。

标签: sql oracle postgresql


【解决方案1】:

是的,您的查询应该有效。如果你想尝试不同的东西,这应该在 Oracle 中工作。很容易将其更改为任何其他 RDBMS。 也很容易添加任何其他主题。

With aux as ( select '%data%' as auxText from dual union all
              select '%informatics%' as auxText from dual union all            
              select '%genomics%' as auxText from dual )
select distinct stud.name, stud.num,
        nvl(stud.sub,'---') as sub
from  table_A left join aux on table_A.sub like aux.auxText;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多