문제

I'm trying to write a query that seems pretty basic in the world of connect by queries. But somehow I'm missing one important piece. I want to know for each "source" id what the ultimate "target" id is. For example, I have this source data:

src trg
1   2
2   3
3   4
7   8

My current incarnation (SQL Fiddle) of the query returns the desired values:

select src, connect_by_root trg ult_trg
from t 
connect by nocycle prior src = trg
order by ult_trg, src;

The problem is that it also returns undesired values. My desired output is this:

1, 4
2, 4
3, 4
7, 8

What is the missing idea I need to limit my result set to the desired rows?

도움이 되었습니까?

해결책

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;

Modified Fiddle

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top