我有1列的表,并有以下数据
Status
a1
i
t
a2
a3
我想在我的选择查询中显示以下结果
Status| STATUSTEXT
a1 | Active
i | Inactive
t | Terminated
a2 | Active
a3 | Active
我能想到的一种方法是在选择查询中使用Switch When表达式
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次活动状态 并且可以在一个中检查整个活动状态单一表达?
答案 0 :(得分:108)
您可以使用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
看一下这个演示
答案 1 :(得分:15)
当然......
select case substr(status,1,1) -- you're only interested in the first character.
when 'a' then 'Active'
when 'i' then 'Inactive'
when 't' then 'Terminated'
end as statustext
from stage.tst
然而,这个架构有一些令人担忧的事情。首先,如果你有一个意味着什么的专栏,那么在最后添加一个数字并不一定是最好的方法。此外,根据您所拥有的状态数,您可能需要考虑将此列转换为单独表的外键。
根据您的评论,您肯定希望将其转换为外键。例如
create table statuses ( -- Not a good table name :-)
status varchar2(10)
, description varchar2(10)
, constraint pk_statuses primary key (status)
)
create table tst (
id number
, status varchar2(10)
, constraint pk_tst primary key (id)
, constraint fk_tst foreign key (status) references statuses (status)
)
您的查询将变为
select a.status, b.description
from tst a
left outer join statuses b
on a.status = b.status
这是一个SQL Fiddle来演示。
答案 2 :(得分:14)
您可以重写它以使用CASE
:
SELECT status,
CASE status
WHEN 'i' THEN 'Inactive'
WHEN 't' THEN 'Terminated'
ELSE 'Active'
END AS StatusText
FROM stage.tst
答案 3 :(得分:4)
使用解码会更容易。
SELECT
status,
decode ( status, 'a1','Active',
'a2','Active',
'a3','Active',
'i','Inactive',
't','Terminated',
'Default')STATUSTEXT
FROM STATUS
答案 4 :(得分:1)
SELECT
STATUS,
CASE
WHEN STATUS IN('a1','a2','a3')
THEN 'Active'
WHEN STATUS = 'i'
THEN 'Inactive'
WHEN STATUS = 't'
THEN 'Terminated' ELSE null
END AS STATUSTEXT
FROM
stage.tst;
答案 5 :(得分:1)
由于网络搜索Oracle case
位于该链接的顶部,因此我在此处添加了案例声明,尽管没有回答关于案例表达式的问题:< / p>
CASE
WHEN grade = 'A' THEN dbms_output.put_line('Excellent');
WHEN grade = 'B' THEN dbms_output.put_line('Very Good');
WHEN grade = 'C' THEN dbms_output.put_line('Good');
WHEN grade = 'D' THEN dbms_output.put_line('Fair');
WHEN grade = 'F' THEN dbms_output.put_line('Poor');
ELSE dbms_output.put_line('No such grade');
END CASE;
或其他变体:
CASE grade
WHEN 'A' THEN dbms_output.put_line('Excellent');
WHEN 'B' THEN dbms_output.put_line('Very Good');
WHEN 'C' THEN dbms_output.put_line('Good');
WHEN 'D' THEN dbms_output.put_line('Fair');
WHEN 'F' THEN dbms_output.put_line('Poor');
ELSE dbms_output.put_line('No such grade');
END CASE;
每个Oracle文档:https://docs.oracle.com/cd/B10501_01/appdev.920/a96624/04_struc.htm
答案 6 :(得分:0)
您只能检查状态的第一个字符。为此,您使用子字符串函数。
substr(status,1,1)
在你的情况下过去。
答案 7 :(得分:0)
以下语法可行:
....
where x.p_NBR =to_number(substr(y.k_str,11,5))
and x.q_nbr =
(case
when instr(substr(y.m_str,11,9),'_') = 6 then to_number(substr(y.m_str,11,5))
when instr(substr(y.m_str,11,9),'_') = 0 then to_number(substr(y.m_str,11,9))
else
1
end
)
答案 8 :(得分:0)
CASE TO_CHAR(META.RHCONTRATOSFOLHA.CONTRATO)
WHEN '91' AND TO_CHAR(META.RHCONTRATOSFOLHA.UNIDADE) = '0001' THEN '91RJ'
WHEN '91' AND TO_CHAR(META.RHCONTRATOSFOLHA.UNIDADE) = '0002' THEN '91SP'
END CONTRATO,
00905. 00000 - "missing keyword"
*Cause:
*Action:
Erro na linha: 15 Coluna: 11
答案 9 :(得分:0)
DECODE(SUBSTR(STATUS,1,1),'a','Active','i','Inactive','t','Terminated','N/A')