如果WHERE子句不匹配,则返回NULL-ORACLE

时间:2018-11-29 12:26:16

标签: sql oracle

我现在大部分时间都在为此而苦苦挣扎。我在Oracle SQL生涯中还比较早,但是我需要返回ldl_act_type.act_type_sc'LID DATE LOGGED'的结果,并为此也提取ldl_act.date_actioned

但是,如果没有找到act_type_sc,我还需要它使这些值为空(本质上仍然返回该行,因为我们需要查看尚未执行的操作)。我一直在摆弄UNIONUNION ALL,但是我认为我不太了解它。希望下面的代码有意义吗?

select 
    incident.incident_ref,
    incident.short_desc "Summary",
    serv_dept.serv_dept_n "Assigned SVD",
    inc_data.event_type,
    incident.date_logged "Date Logged",
    ldl_act.date_actioned "LID Date Logged",
    prp_act.date_actioned "Proposed LID Date",
    prp_act.remarks "Proposed LID Comments"
from 
    incident
    inner join serv_dept on incident.ass_svd_id = serv_dept.serv_dept_id
    inner join inc_data on incident.incident_id = inc_data.incident_id
    left join act_reg ldl_act on incident.incident_id = ldl_act.incident_id
    left join act_reg prp_act on incident.incident_id = prp_act.incident_id
    left join act_type ldl_act_type on ldl_act.act_type_id = 
ldl_act_type.act_type_id
    left join act_type prp_act_type on prp_act.act_type_id = 
prp_act_type.act_type_id
where 
    (inc_data.event_type = 'p')
    and (incident.date_logged > (sysdate-30))
    and (prp_act_type.act_type_sc = 'PROPOSED LID DATE')
    and (ldl_act_type.act_type_sc = 'LID DATE LOGGED')

2 个答案:

答案 0 :(得分:0)

因此,如果我没看错,您是否希望ldl_act_type.act_type_sc = 'LID DATE LOGGED'出现该行或该行不存在?这有点令人困惑,因为您act_type参加了两次,但我不清楚他们之间的关系。

所以您不是只想将最后一个子句更改为:

and ( ldl_act_type.act_type_sc = 'LID DATE LOGGED' OR 
      ldl_act_type.act_type_sc IS NULL )

答案 1 :(得分:0)

select incident.incident_ref,
incident.short_desc "Summary",
serv_dept.serv_dept_n "Assigned SVD",
inc_data.event_type,
incident.date_logged "Date Logged",
ldl_act.date_actioned "LID Date Logged",
prp_act.date_actioned "Proposed LID Date",
prp_act.remarks "Proposed LID Comments"
from incident
inner join serv_dept on incident.ass_svd_id = serv_dept.serv_dept_id
inner join inc_data on incident.incident_id = inc_data.incident_id
left join (select ldl_act.incident_id,ldl_act_type.act_type_sc from act_reg ldl_act 
            inner join act_type ldl_act_type on ldl_act.act_type_id = 
ldl_act_type.act_type_id) 
            as ldl_act_type on incident.incident_id = ldl_act.incident_id
left join (select prp_act.incident_id,prp_act_type.act_type_sc from act_reg prp_act 
            inner join act_type prp_act_type on prp_act.act_type_id = 
prp_act_type.act_type_id) 
            as prp_act_type on incident.incident_id = prp_act.incident_id
where (inc_data.event_type = 'p')
and (incident.date_logged > (sysdate-30))
and (prp_act_type.act_type_sc = 'PROPOSED LID DATE')
and (ldl_act_type.act_type_sc = 'LID DATE LOGGED')

您可以尝试吗?