What is the best way to fetch Tree/Hierarchical structure data from Oracle
I want to fetch Hierarchical/Tree data something like below from a Table which has following definiton.
Tree Table:
"""""""""""
Id |ParentId
"""""""""""
Work1|null
Work2|Work1
Work3|Work2
...
Required Query result Data (no need to be tabbed)- If I Pick 'Work1' I should complete Ids which are under its root something like below. If I pick 'Work2' then also I should complete Ids above and below its root.
> Work1
----开发者_如何学编程------
> Work2
----------
> Work3
---------
I used below query but it is giving me duplicate records.
select Id from TreeTable start with Id in ('Work1','Work2') connect by nocycle Parentid=prior Id or Id = prior Parentid
Note: I want to get the data within a SINGLE QUERY (optimized) way
Thanks and Regards, PV.
SELECT id
FROM q
START WITH
id IN ('Work1', 'Work2')
CONNECT BY
parent = PRIOR id
AND id NOT IN ('Work1', 'Work2')
This condition:
AND id NOT IN ('Work1', 'Work2')
will cut off the branches already selected with START WITH
.
精彩评论