我有以下两个版本的ANSI兼容SQL(列/表名称已更改以保护机密数据),其中一个版本通过遵循正确的逻辑来满足我的要求,而另一个则没有。
1)ANSI Join 1-Works
SELECT b.COLUMN_A,
COUNT(a.COLUMN_A)
FROM TABLE1 a
RIGHT OUTER JOIN
(SELECT COLUMN_A FROM TABLE2 WHERE COLUMN_X='TEST') b
ON a.COLUMN_A = b.COLUMN_A
AND a.COLUMN_B in (SELECT FROM TABLE3 WHERE COLUMN_Y=5) --WORKS
GROUP BY b.COLUMN_A
1)给出如下输出:
COLUMN_A COUNT(COLUMN_A)
--------------------------
A 0
B 0
C 1
D 1
E 0
2)ANSI加入2 - 不起作用
SELECT b.COLUMN_A,
COUNT(a.COLUMN_A)
FROM TABLE1 a
RIGHT OUTER JOIN
(SELECT COLUMN_A FROM TABLE2 WHERE COLUMN_X='TEST') b
ON a.COLUMN_A = b.COLUMN_A
WHERE
a.COLUMN_B in (SELECT FROM TABLE3 WHERE COLUMN_Y=5) --DOESN'T WORK
GROUP BY b.COLUMN_A
3)Oracle的专有连接 - 不起作用
SELECT b.COLUMN_A,
COUNT(a.COLUMN_A)
FROM TABLE1 a,(SELECT COLUMN_A FROM TABLE2 WHERE COLUMN_X='TEST') b
WHERE
a.COLUMN_A(+) = b.COLUMN_A
AND a.COLUMN_B in (SELECT FROM TABLE3 WHERE COLUMN_Y=5) --DOESN'T WORK
GROUP BY b.COLUMN_A
2)& 3)输出如下:
COLUMN_A COUNT(COLUMN_A)
--------------------------
C 1
D 1
我理解(2,ANSI)& (3,所有权)是等同的。但是(1,ANSI)是否有任何等效的专有SQL? 任何帮助都是最受欢迎的。 谢谢。 编辑:我已使用示例输出更新了问题。
答案 0 :(得分:6)
您可能想要更改此条件:
AND a.COLUMN_B in (SELECT FROM TABLE3 WHERE COLUMN_Y=5)
要么:
AND a.COLUMN_B (+) in (SELECT FROM TABLE3 WHERE COLUMN_Y=5)
或者这个:
AND (a.COLUMN_B IS NULL OR a.COLUMN_B in (SELECT FROM TABLE3 WHERE COLUMN_Y=5))
但总的来说,我会说使用ANSI语法进行外连接。即使作为首先学习Oracle语法并且非常熟悉它的人,ANSI对于外连接也更加清晰。