[英]Using select case on a left join?
I have used a left join on two of my tables. 我在两个表上使用了左联接。 Now I want to use case to identify the records from my left table who don't have a match in the right table.
现在,我想用用例来识别左表中没有匹配项的记录。 Such records exist and have a null value in the 'id_zeus' column of my join, however when I execute the case, it is as these fields don't exist.
这些记录存在,并且在我的联接的“ id_zeus”列中具有空值,但是,当我执行该案例时,是因为这些字段不存在。 Where am I going wrong ?
我要去哪里错了? I get "Present" in all my column Disturbance.
我在所有干扰专栏中都看到“存在”。 I am using Oracle SQL developer.
我正在使用Oracle SQL开发人员。
SELECT
CASE DP.ID_PRB
WHEN NULL
THEN 'Absence'
ELSE 'Present' END as Disturbance,
FROM
FIRE.WSITE WI
LEFT JOIN
(SELECT DISTINCT
DPL.ID_PERT as ID_PRB
FROM FIRE.DEPPLAN DPL
GROUP BY DPL.ID_PERT
) DPL
ON WI.ID_PERT = DP.ID_PERT
What is const
? 什么是
const
? You don't seem to need it. 您似乎不需要它。 The
SELECT DISTINCT
and GROUP BY
are redundant, so use only one of them. SELECT DISTINCT
和GROUP BY
是多余的,因此只能使用其中之一。 And your alias on the subquery is incorrect. 并且您在子查询上的别名不正确。
But your problem is the comparison to NULL
. 但是您的问题是与
NULL
的比较。 It doesn't even match when doing a comparison as you are doing in CASE
. 就像在
CASE
一样,进行比较时甚至不匹配。 You need to use IS NULL
: 您需要使用
IS NULL
:
SELECT (CASE WHEN DP.ID_PRB IS NULL THEN 'Absence' ELSE 'Present'
END) as Disturbance,
FROM FIRE.WSITE WI LEFT JOIN
(SELECT DISTINCT DPL.ID_PERT as ID_PRB
FROM FIRE.OSI_DEVIATION_PLANS DP
) DP
ON WI.ID_PERT = DP.ID_PERT;
This query would commonly be written as: 该查询通常写为:
SELECT (CASE WHEN NOT EXISTS (SELECT 1
FROM FIRE.OSI_DEVIATION_PLANS DP
WHERE WI.ID_PERT = DP.ID_PERT
)
THEN 'Absence' ELSE 'Present'
END) as Disturbance,
FROM FIRE.WSITE WI ;
This offers more opportunities for optimization. 这为优化提供了更多机会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.