2015-12-03 205 views
0

我有一個表有許多項目的ID,併爲每一個項目,我有層次結構如下所示。層次查詢 - 甲骨文

enter image description here

我輸入我的風險id..How可以得到輸入風險ID頂層父。

如果我給25作爲輸入,我應該得到23 output.how我能做到這一點。

+0

你能告訴我們你試過了什麼嗎?另外,作爲提示,查找CTE(公用表表達式)。 –

+1

這個herarchy是如何定義的?什麼是link1和link2? – miracle173

回答

0

從所需行開始上升到根:

SELECT RiskID, 
FROM PROJECT_RISKS 
WHERE CONNECT_BY_ISLEAF = 1 
START WITH 
     RiskID = 25 
CONNECT BY 
     PRIOR Link2 = Link1 
AND PRIOR ProjectID = ProjectID; 

從根開始並下降到所需的行:

SELECT CONNECT_BY_ROOT(RiskID) AS RiskID 
FROM Project_Risks 
WHERE RiskID = 25 
START WITH 
     link2  IS NULL 
CONNECT BY 
     PRIOR Link1 = Link2 
AND PRIOR ProjectID = ProjectID; 

兩者都會給出相同的答案。