Oracle SQL Left连接相同的表未知次数

时间:2018-02-26 13:21:50

标签: sql oracle dynamic left-join

我有这张桌子

| old  | new   | 
|------|-------| 
| a    | b     | 
| b    | c     | 
| d    | e     | 
| ...  | ...   | 
| aa   | bb    | 
| bb   | ff    | 
| ...  | ...   | 
| 11   | 33    | 
| 33   | 523   | 
| 523  | 4444  | 
| 4444 | 21444 | 

我想要实现的结果是

| old  | newest | 
|------|--------| 
| a    | e      | 
| b    | e      | 
| d    | e      | 
| ...  |        | 
| aa   | ff     | 
| bb   | ff     | 
| ...  |        | 
| 11   | 21444  | 
| 33   | 21444  | 
| 523  | 21444  | 
| 4444 | 21444  | 

我可以对查询进行硬编码以获得我想要的结果。

SELECT 
   older.old,
   older.new,
   newer.new firstcol,
   newer1.new secondcol,
   …
   newerX-1.new secondlastcol,
   newerX.new lastcol

from Table older

Left join Table newer 
on older.old = newer.new

Left join Table newer1 
on newer.new = newer1.old

…

Left join Table newerX-1 
on newerX-2.new = newerX-1.old

Left join Table newerX 
on newerX-1.new = newerX.old;

然后只取右边的第一个非空值。

这里有说明:

| old  | new   | firstcol | secondcol | thirdcol | fourthcol |     | lastcol | 
|------|-------|----------|-----------|----------|-----------|-----|---------| 
| a    | b     | c        | e         | null     | null      | ... | null    | 
| b    | c     | e        | null      | null     | null      | ... | null    | 
| d    | e     | null     | null      | null     | null      | ... | null    | 
| ...  | ...   | ...      | ...       | ...      | ...       | ... | null    | 
| aa   | bb    | ff       | null      | null     | null      | ... | null    | 
| bb   | ff    | null     | null      | null     | null      | ... | null    | 
| ...  | ...   | ...      | ...       | ...      | ...       | ... | null    | 
| 11   | 33    | 523      | 4444      | 21444    | null      | ... | null    | 
| 33   | 523   | 4444     | 21444     | null     | null      | ... | null    | 
| 523  | 4444  | 21444    | null      | null     | null      | ... | null    | 
| 4444 | 21444 | null     | null      | null     | null      | ... | null    | 

问题在于“替换链”的长度总是在变化(可以从10变化到100)。

必须有更好的方法来做到这一点吗?

1 个答案:

答案 0 :(得分:0)

您正在寻找的是递归查询。像这样:

with cte (old, new, lev) as
(
  select old, new, 1 as lev from mytable
  union all
  select m.old, cte.new, cte.lev + 1
  from mytable m
  join cte on cte.old = m.new
)
select old, max(new) keep (dense_rank last order by lev) as new
from cte
group by old
order by old;

递归CTE创建所有迭代(您可以通过select * from cte替换查询来看到这一点)。在最后的查询中,我们得到了Oracle new的每oldKEEP LAST

Rextester演示:http://rextester.com/CHTG34988