Using CONNECT BY to get all parents and one child in Hierarchy through SQL query in Oracle

Posted by s khan on Stack Overflow See other posts from Stack Overflow or by s khan
Published on 2010-03-31T07:41:38Z Indexed on 2010/03/31 7:43 UTC
Read the original article Hit count: 179

Filed under:
|
|

I was going through some previous posts on CONNECT BY usage. What I need to find is that what to do if I want to get all the parents (i.e, up to root) and just one child for a node, say 4.

It seems Like I will have to use union of the following two:-

 SELECT  * 
 FROM    hierarchy 
 START WITH id = 4
 CONNECT BY id = PRIOR parent
union
     SELECT  *
     FROM    hierarchy
     WHERE LEVEL =<2
     START WITH
     id = 4
     CONNECT BY
     parent = PRIOR id

Is there a better way to do this, some workaround that is more optimized?

© Stack Overflow or respective owner

Related posts about sql

Related posts about Oracle