[英]How do I create two additional fields to a view based on output from two case statements
Hi I'm looking to create two additional fields with my query. 嗨,我想用我的查询创建另外两个字段。 Status (ACTIVE/INACTIVE) and REASON (BAU/EXPIRY) based on criteria.
状态(活动/无效)和原因(BAU / EXPIRY)基于条件。 Currently it is one column, but I want to split it into two.
当前是一列,但我想将其分为两部分。 Below is the code:
下面是代码:
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
Want it to display 希望它显示
**STATUS** **REASON**
ACTIVE
ACTIVE
INACTIVE CONTRACT EXPIRY
INACTIVE BAU
... ...
based on your sample... 根据您的样品...
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)
do this 做这个
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
I don't normally answer my own question but below was what I was looking for 我通常不会回答自己的问题,但是下面是我在寻找什么
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.