![](/img/trans.png)
[英]SQL: Given one record, how to find all its associated child records without knowing the number of levels in the hierarchy?
[英]How to define the number of levels in a hierarchy using sql?
我的情況如下。 我有一個包含產品層次結構的表。 下表是數據集的摘錄:
child parent
1 2
2 3
4 5
6 7
我想添加一列,其中包含層次結構的(子級)深度。 如下所示:
child parent depth
1 2 2
2 3 1
4 5 1
6 7 1
我將如何在Oracle中做到這一點? 謝謝!
就像是:
SELECT child, parent, level
FROM your_table
START WITH parent NOT IN ( SELECT child FROM your_table )
CONNECT BY parent = PRIOR child;
輸出:
CHILD PARENT LEVEL
----- ------ -----
1 2 2
2 3 1
4 5 1
6 7 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.