Perbarui
Setelah melihat pembaruan, Anda hanya perlu menghapus START WITH.
Akhiri pembaruan
CONNECT_BY_ROOT adalah yang Anda cari
Pernyataan ini
SELECT distinct RootBoss, Person FROM (
with employee as
(
Select 1 person , null boss from Dual
UNION Select 2 , 1 from dual
UNION Select 3 , 2 from dual
)
SELECT CONNECT_BY_ROOT boss RootBoss, person
FROM employee connect_by
connect by prior person = boss
ORDER BY person
) t
WHERE ROOTBOSS is not null
ORDER BY
RootBoss, Person
Keluaran
ROOTBOSS PERSON
---------------------- ----------------------
1 2
1 3
2 3
Menambahkan SUMS dan GROUPS cukup mudah