我有一个源表(T1):
ID1 | ID2
----------
1 | 2
1 | 5
4 | 7
7 | 8
9 | 1
我想将数据转换为此(T2):
ID1 | ID2 | LABEL
------------------
1 | 2 | 1
1 | 5 | 1
4 | 7 | 2
7 | 8 | 2
9 | 1 | 1
我在 PostgreSQL 中找到了一个解决方案:
with
recursive cte(id1, id2) as (
select id1, id2, 1 as level
from t
union all
select t.id1, cte.id2, cte.level + 1
from t join
cte
on t.id2 = cte.id1
)
select id1, id2,
dense_rank() over (order by grp) as label
from (select id1, id2,
least(min(id2) over (partition by id1), min(id1) over (partition by id2)) as grp,
level
from cte
) t
where level = 1;
我想将此代码转换为 Oracle。如何将此代码从 Postgres 转换为 Oracle?