2

I have a table(lets name it HEIRARCHY_TABLE ) in this format representing hierarchical data where a tree is stored in this format.

HEIRARCHY_TABLE (parent_name ,parent_id     ,child_name ,child_id)
Sample Data in HEIRARCHY_TABLE:-
-------------------------------------------
parent_name |parent_id     |child_name |child_id
--------------------------------------------
parent_1  | parent_1_id   | leaf_node |  leaf_node_id1

parent_2   | parent_2_id   | parent_1  |  parent_1_id   

the above scenario is showing data for a case where parent_2->parent_1->leaf_node where -> representing "is parent of " relation-ship.

I need to query this table and get a result like this for all leaf nodes. Result:-

leaf_node | parent_1 | parent_2 | parent_3 |parent_4 | parent_5 |parent_6 | parent_7

and if for suppose a leaf node has only two parents then i require the rest of the parent values to be null. i.e..,if it has only 5 parents.then parent_6 and parent_7 should be null. Note :- the above table contains multiple trees.hence it contain multiple roots.I need data for all the trees available in this table in this format.the maximum level in all the trees is 7 only.

4

2 回答 2

0

最快的查询是使用层次 ID。然后您可以使用层次结构 id 列上的 CONTAINS 查询全部或部分树。

当然,这假设您被允许/可以构建此列。

对于 oracle,您可以执行层次结构查询。不确定它的效率/非正规化因素。http://docs.oracle.com/cd/B19306_01/server.102/b14200/queries003.htm

于 2013-07-11T22:03:11.040 回答
0

谷歌搜索通用表表达式。我认为你正在寻找的是一个递归查询。您将不得不玩一些游戏来强制查询为实际上不存在的父母返回空值。[检查这个。](http://www.morganslibrary.org/reference/with.html

于 2013-07-11T22:03:54.523 回答