开始......先前连接

时间:2014-02-14 15:59:49

标签: sql oracle connect-by

有没有人知道如何知道CONNECT BY的深度?例如,如果我有;

PARENT_ID   PARENT_KEY  CHILD_ID    CHILD_KEY
1           BOB         2           CHRIS
1           BOB         3           TODD
2           CHRIS       4           JON
2           CHRIS       5           ALF
5           ALF         6           GERARD
5           ALF         7           MIKE
3           TODD        8           ANN
3           TODD        9           FRED

CHRIS的深度为3,TODD的深度为2,如何以编程方式找出CHRIS和TODD的深度;

START FOR PARENT_KEY ='BOB'INTRECT by PRIOR CHILD_ID = PARENT_ID

对不起,我是傻瓜......这是未来有脑瘫痪的人的答案;

SELECT
PARENT_KEY
, MAX(LEVEL) AS DEPTH
FROM TIMELINE_CONFIG
START WITH PARENT_KEY = 'BOB' CONNECT BY PRIOR CHILD_ID = PARENT_ID
GROUP BY PARENT_KEY

2 个答案:

答案 0 :(得分:4)

您可以使用LEVEL伪列:

SELECT ..., LEVEL, ...
FROM ...

答案 1 :(得分:0)

对不起,我是傻瓜......这是未来有脑瘫痪的人的答案;

SELECT
PARENT_KEY
, MAX(LEVEL) AS DEPTH
FROM TIMELINE_CONFIG
START WITH PARENT_KEY = 'BOB' CONNECT BY PRIOR CHILD_ID = PARENT_ID
GROUP BY PARENT_KEY