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: Newbie: sql query

Re: Newbie: sql query

From: Gints Plivna <gints.plivna_at_gmail.com>
Date: 28 Jun 2006 07:41:31 -0700
Message-ID: <1151505691.718357.142550@i40g2000cwc.googlegroups.com>


gfiuni2_at_gmail.com wrote:
> Hello,
>
> Given the table below:
>
> id | parent_id
> -------------------------
> 1 | NULL
> 7 | 1
> 9 | 7
> 13 | 9
> 16 | 13
> .....
>
>
> I want to get the id of the fisrt register (id=1) given any other id
> (7, 9 ,13, ..). It is posible to do it with a SQL query or do I need to
> use PL/SQL?
>
> Thanks in advance,
> Jose Luis.

Something like that probably will help:

select id from (
select id, level lvl from <your table>
start with id = <put your id here>
connect by prior parent_id = id
order by level desc
)
where rownum <=1
/
There are problems with this query if child has many parents.

Anyway you should look into Hierarchical Queries.

Gints Plivna Received on Wed Jun 28 2006 - 09:41:31 CDT

Original text of this message

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