![](/img/trans.png)
[英]Using Oracle how do I conditionally select a column from two different tables based on two difference if statements?
[英]How do I create two additional fields to a view based on output from two case statements
嗨,我想用我的查询创建另外两个字段。 状态(活动/无效)和原因(BAU / EXPIRY)基于条件。 当前是一列,但我想将其分为两部分。 下面是代码:
select first_name,last_name,end_date as "Contract end date",date_removed,
case
when END_DATE > sysdate and DATE_REMOVED is null then 'ACTIVE'
when END_DATE <= sysdate then 'INACTIVE - Contract Expired'
when DATE_REMOVED is not null then 'INACTIVE - BAU'
end
as status
from DEPARTMENTS d inner join employees e on e.DEPARTMENT_ID = d.DEPARTMENT_ID
希望它显示
**STATUS** **REASON**
ACTIVE
ACTIVE
INACTIVE CONTRACT EXPIRY
INACTIVE BAU
... ...
根据您的样品...
select
case when STATUS like '%ACTIVE%' is null then '' ,
when STATUS like '%INACTIVE%' and DATE_REMOVED is not null then 'BAU'
when STAUS like '%INACTIVE%' and END_DATE <= sysdate then 'Contract Expired'
end as REASON,
case when REASON IS NULL then 'ACTIVE'
ESLE 'INACTIVE' end as STATUS
from (
select first_name,last_name,end_date as "Contract end date",date_removed,
case
when END_DATE > sysdate and DATE_REMOVED is null then 'ACTIVE'
when END_DATE <= sysdate then 'INACTIVE - Contract Expired'
when DATE_REMOVED is not null then 'INACTIVE - BAU'
end
as status
from DEPARTMENTS d inner join employees e on e.DEPARTMENT_ID = d.DEPARTMENT_ID)
做这个
select a.*, substr(a.status,replace(instr(a.status,'-',1,1),0,LENGTH(a.status)) + 1) Reason from (select first_name,last_name,end_date as "Contract end date",date_removed,
case
when END_DATE > sysdate and DATE_REMOVED is null then 'ACTIVE'
when END_DATE <= sysdate then 'INACTIVE - Contract Expired'
when DATE_REMOVED is not null then 'INACTIVE - BAU'
end
as status
from DEPARTMENTS d inner join employees e on e.DEPARTMENT_ID = d.DEPARTMENT_ID) a
我通常不会回答自己的问题,但是下面是我在寻找什么
select first_name,last_name,end_date,date_removed,
case
when END_DATE > sysdate and DATE_REMOVED is null then 'ACTIVE'
when END_DATE <= sysdate then 'INACTIVE'
end
as status,
case
when END_DATE <= sysdate then 'Contract Expired'
when DATE_REMOVED is not null then 'BAU'
end
as Reason
from DEPARTMENTS d inner join employees e on e.DEPARTMENT_ID = d.DEPARTMENT_ID;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.