我正在尝试编写一个在connect by
查询世界中看起来非常基本的查询。但不知何故,我错过了一个重要的部分。我想知道每个"源"ID的最终"目标"id是什么。例如,我有以下源数据:
src trg
1 2
2 3
3 4
7 8
我查询的当前化身(SQL Fiddle)返回所需的值:
select src, connect_by_root trg ult_trg
from t
connect by nocycle prior src = trg
order by ult_trg, src;
问题是它还返回不需要的值。我想要的输出是这样的:
1, 4
2, 4
3, 4
7, 8
我需要将结果集限制为所需行的缺失想法是什么?
select
connect_by_root src as src,
trg ult_trg
from t
where
connect_by_isleaf = 1
connect by nocycle src = prior trg
order by ult_trg, src
select src, MAX(connect_by_root trg) ult_trg
from t
connect by nocycle prior src = trg
GROUP BY src
order by ult_trg, src;
改良小提琴