如何在oracle中进行自我外连接? (我正在使用11g)
我尝试了以下查询,其中包含(+)到处和左/右的许多组合:
select t1.name, t1.status, t2.status
from my_tab t1 outer join my_tab t2
on t1.name = t2.name
where t1.year= X and t2.year = Y and t1.status <> t2.status;
预期结果:在X和Y年之间改变身份的人,以及新人和失踪者
只有改变状态的人
Oracle doc说
您不能使用(+)运算符将表外连接到自身
它是否仅适用于(+)运算符,还是应该读取“你不能将表连接到自身”?
答案 0 :(得分:0)
肮脏的解决方案:
--request presented above
select t1.name, t1.status as status_X, t2.status as status_Y
from my_tab t1 join my_tab t2
on t1.name = t2.name
where t1.year = X and t2.year = Y and t1.status <> t2.status
union
--stuff that disappeared from X to Y
select name, status, null
from my_tab
where year = X and name not in (select name from my_tab where year = Y)
union
--new stuff
select name, null, status
from my_tab
where year = Y and name not in (select name from my_tab where year = X)