Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: Efficiently getting top level parent in hierarchy with connect by

Re: Efficiently getting top level parent in hierarchy with connect by

From: Mike C <michaeljc70_at_hotmail.com>
Date: 12 Oct 2006 12:40:44 -0700
Message-ID: <1160682044.786348.229590@c28g2000cwb.googlegroups.com>

Mike C wrote:
> I have a query on a simple table that has parent child relationships.
> I need a query to get the highest level parent for a child. Is there a
> simpler way to do this than what I have which repeats the query to get
> the max level?
>
> select parent_id,level from tbl_parent_child g
> WHERE level=(SELECT MAX(level) FROM tbl_parent_child start with
> child_id = 139527170 connect by child_id = prior parent_id )
> start with child_id = 139527170 connect by child_id = prior parent_id

The best I came up with using Oracle 9 is this: select parent_id
FROM (select parent_id, RANK() OVER (ORDER BY level DESC) rnk from tbl_parent_child
start with child_id = 139527170 connect by child_id = prior parent_id ) where rnk=1 Received on Thu Oct 12 2006 - 14:40:44 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US