简体   繁体   English

如何基于两个case语句的输出为视图创建两个其他字段

[英]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.

相关问题 使用 Oracle 如何根据两个不同的 if 语句有条件地从两个不同的表中选择一列? - Using Oracle how do I conditionally select a column from two different tables based on two difference if statements? 包含两个不同字段的两个条件的case语句 - case statements with two conditions of two different fields 如何根据两个不同的 case 语句获得 Datediff 计算? - How to get a Datediff calculation based on two different case statements? 我该如何匹配两个表中的三个字段,以便在匹配的情况下更新相同的表 - How do i go about matching three fields from two tables in order to update the same tables in case of match 如何根据 CASE 执行不同的 SELECT 语句 - How do I execute different SELECT statements based on a CASE 如何通过联接PostgreSQL中的两个表来创建视图? - How do I create a view by joining two tables in PostgreSQL? 如何连接两个表并使用逗号分隔值创建一个附加列 - How do I join two tables and create an additional column with comma seperated values 减去两个案件陈述 - Subtracting Two Case Statements 如何在sql view语句的输出中合并显示为一个的两个字段? - How can i merge two fields shown as one in an output of an sql view statement? 如何生成一个新列,其值基于一列的分区,同时还基于另外两列 (BigQuery) 的值? - How do I produce a new column with values based on a Partition of one column while also based on the values of two additional columns (BigQuery)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM