CASE .. WHEN在Oracle SQL中的表达式 [英] CASE .. WHEN expression in Oracle SQL

查看:64
本文介绍了CASE .. WHEN在Oracle SQL中的表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格有1列,并且包含以下数据

I have the table with 1 column and has following data

Status
a1
i
t
a2
a3

我想在我的选择查询中显示以下结果

I want to display the following result in my select query

Status| STATUSTEXT
a1    | Active
i     | Inactive
t     | Terminated
a2    | Active
a3    | Active

我可以想到的一种方法是在选择查询中使用Switch when表达式

One way I could think was using a Switch When expression in select query

SELECT
status,
CASE status 
WHEN 'a1' THEN 'Active'
WHEN 'a2' THEN 'Active'
WHEN 'a3' THEN 'Active'
WHEN 'i' THEN 'Inactive'
WHEN 't' THEN 'Terminated'
END AS StatusText
FROM stage.tst

还有其他方法可以不用写 表达式3次表示活动状态" 并且可以一次检查整个活动状态单一表达?

Is there any other way of doing this where I don't need to write When expression 3 times for Active Status and the entire active status can be checked in one single expression?

推荐答案

您可以使用IN子句

类似

SELECT
  status,
  CASE
    WHEN STATUS IN('a1','a2','a3')
    THEN 'Active'
    WHEN STATUS = 'i'
    THEN 'Inactive'
    WHEN STATUS = 't'
    THEN 'Terminated'
  END AS STATUSTEXT
FROM
  STATUS

看看这个演示

这篇关于CASE .. WHEN在Oracle SQL中的表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆