Oracle SQL如果有时不存在,该如何选择(或包含数据)?

时间:2019-10-11 13:15:46

标签: sql oracle

当我在视图中选择数据时, 在大多数情况下,我将获得数据:

Referred Job_Status     Jobs
OPS      DISPATCHED     9
OPS      NOT_DISPATCHED 3

但是有时候数据不存在,我只能得到这个:

Referred Job_Status     Jobs
OPS      DISPATCHED     9

当它不存在时,我仍然想将其显示为零:

Referred Job_Status     Jobs
OPS      DISPATCHED     9  
OPS      NOT_DISPATCHED 0

对不起,我有一个星期五早上,我无法直截了当。

1 个答案:

答案 0 :(得分:2)

您可以尝试以下操作:

with main as (
select 'OPS' Referred ,'DISPATCHED' Job_Status FROM DUAL
UNION
select 'OPS' Referred ,'NOT_DISPATCHED ' Job_Status FROM DUAL
)
SELECT
MAIN.Referred ,MAIN.Job_Status , NVL(SUB.JOBS,0) JOBS
FROM MAIN LEFT OUTER JOIN YOUR_VIEW SUB ON  MAIN.REFERRED=SUB .REFERRED 
                                        AND MAIN.Job_Status =SUB.Job_Status ;