具有相关性的分层查询

时间:2017-01-17 18:09:10

标签: sql oracle hierarchical connect-by

我有2个表IndexTab,ChildNodes。 Index选项卡有一个名为“Index”的coljumn,它给出了唯一值

索引

150
160
170

这些值被截断并在每次执行时随机填充。该表与下面的子表

相关

的childNodes

+----+------------+--------+
| ID | ChildValue | Parent |
+----+------------+--------+
|  1 | Child 1    |    150 |
|  2 | Child 2    |      1 |
|  3 | Child 3    |      1 |
|  4 | Child 4    |      2 |
|  5 | Child 5    |      2 |
|  6 | Child 6    |      3 |
|  7 | Child 7    |    160 |
|  8 | Child 8    |      7 |
|  9 | Child 9    |      7 |
| 10 | Child 10   |      8 |
| 11 | Child 11   |      9 |
| 12 | Child 12   |      9 |
+----+------------+--------+

现在我想要分层查询,这可以用Correlation编写。下面是我想写的样本......

SELECT ChildValue, ID, Parent
FROM ChildNodes
START WITH ID = IndexTab.Index
CONNECT BY PRIOR ID = Parent

1 个答案:

答案 0 :(得分:1)

目前尚不清楚你想得到什么。

我的假设是你需要这样的东西:

SELECT ChildValue, ID, Parent
FROM ChildNodes
START WITH Parent IN (select Index from IndexTab)
CONNECT BY PRIOR ID = Parent