我希望根據CONNECT BY查詢結果創建一個自動編號的大綱。
如果我的查詢結果如下:
level col1
----- --------
1 text1
1 text2
2 text3
3 text4
3 text5
1 text6
我對匯出數字層次值感興趣,就像這樣:
我對匯出數字層次值感興趣。
level outline col1
----- ------- --------
1 1 text1
1 2 text2
2 2.1 text3
3 2.1.1 text4
3 2.10.2 text5
1 3 text6
它感覺像是一個sys_connect_by_path或視窗化的lag - 但我沒有看到它...
uj5u.com熱心網友回復:
你沒有提供測驗資料,所以我將在scott.emp表上代替說明。
select level,
substr(sys_connect_by_path(rn, '. ' ), 2) as outline,
經驗
from (
select empno, mgr,
row_number() over ( partition by mgr order by empno) as rn
from scott.emp
)
start with mgr is null
連接 by mgr = prior empno
order siblings by empno
;
級別大綱empno
----- -------------- -----
1 1 7839
2 1.1 7566[/span].
3 1.1.1 7788
4 1.1.1 .1 7876
3 1.1.2 7902[/span
4 1.1.2.1 7369[/span
2 1.2 7698[/span].
3 1.20.1 7499
3 1.2.2 7521
3 1.2.3 7654
3 1.2.4 7844[/span
3 1.2.5 7900
2 1.3 7782[/span].
3 1.30.1 7934
在子查詢中,我們給 "兄弟姐妹"(具有相同直接父輩的行/雇員)一個順序號,并在sys_connect_by_path中使用該順序。為了從分層查詢中獲得 "正確的 "排序,你需要按照你在子查詢中的排序方式對兄弟姐妹進行排序(在我的例子中,通過empno,這是主鍵;在你的例子中,如果col1可能有重復的,在兩個地方通過col1, rowid排序以打破聯系)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/308126.html
標籤:
上一篇:一個程序的孫子和曾孫子
