2012-11-06 36 views
1

如何在oracle中進行自我外連接? (我使用11克)如何在Oracle中進行自我外連接?

我嘗試下面的查詢,有很多的組合(+)無處不在,左/右:

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之間改變狀態,以及新的人,誰消失

人只拿到了誰改變了身份的人


甲骨文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)