LEVEL伪列
atlasdb=# SELECT id, parent_id, Level a,
atlasdb-# sys_connect_by_path (TO_CHAR (id), '/') AS Path
atlasdb-# FROM sys_cbp_test2
atlasdb-# START WITH parent_id IS NULL CONNECT BY prior id = parent_id;
id | parent_id | a | path
----+-----------+---+-----------
1 | | 1 | /1
20 | | 1 | /20
2 | 1 | 2 | /1/2
5 | 1 | 2 | /1/5
21 | 20 | 2 | /20/21
3 | 2 | 3 | /1/2/3
6 | 5 | 3 | /1/5/6
7 | 2 | 3 | /1/2/7
22 | 21 | 3 | /20/21/22
4 | 3 | 4 | /1/2/3/4
(10 rows)