Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Newbie: sql query
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