[英]show the hierarchy of tree by showing path to leaf node
我想在oracle中編寫查詢,以便可以將我的樹層次結構排成一行,並且僅顯示最后一個節點路徑,就像我有一個ID及其父ID。
id parent_id
1 null
2 1
3 2
4 3
5 4
6 5
並且輸出應該像路徑一樣成一行
1-2-3-4-5-6
使用函數sys_connect_by_path()
和偽列connect_by_is_leaf
:
select id, ltrim(sys_connect_by_path(id, '-'), '-') path
from test where connect_by_isleaf = 1
connect by prior id = parent_id
start with parent_id is null
輸出和SQLFiddle :
ID PATH
--- ---------------
6 1-2-3-4-5-6
編輯: where
子句在這里很自然,但是由於某些原因您絕對不想要它。 您可以使用:
select id, path from (
select id, ltrim(sys_connect_by_path(id, '-'), '-') path, connect_by_isleaf leaf
from test connect by prior id = parent_id
start with parent_id is null)
connect by 1=0 start with leaf = 1
select LISTAGG(id, '-') WITHIN GROUP (ORDER BY id) from trea_sort connect by PRIOR id = parent start with parent is null
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.