Home » RDBMS Server » Server Administration » Hirarchy list query ( Generation)
icon5.gif  Hirarchy list query ( Generation) [message #122125] Fri, 03 June 2005 02:03 Go to next message
sareeshu
Messages: 1
Registered: June 2005
Location: India
Junior Member
I have table contains the fields (Id, parentid )

the values in the table are like...

id parentid
1 0
2 1
3 1
4 2

if i pass 4 then it shud return 4's parent...its parent and again parent's parent means 2, 1, 0
and if i paas 2 it shud give 1, 0

I need a single query to solve this problem ( not pl/sql).

please let me know if any solution for this.
Re: Hirarchy list query ( Generation) [message #122145 is a reply to message #122125] Fri, 03 June 2005 06:12 Go to previous message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
Use a hierarchical query:

SQL> select * from mhe_tab;

        ID   PARENTID
---------- ----------
         1          0
         2          1
         3          1
         4          2

SQL> select parentid
  2    from mhe_tab
  3  connect by id = prior parentid
  4    start with id = &the_id
  5  /
Enter value for the_id: 4
old   4:   start with id = &the_id
new   4:   start with id = 4

  PARENTID
----------
         2
         1
         0

SQL> /
Enter value for the_id: 2
old   4:   start with id = &the_id
new   4:   start with id = 2

  PARENTID
----------
         1
         0


MHE
Previous Topic: I want to partition Adv Queue table but I get ORA-02216
Next Topic: rmjvm.sql errors
Goto Forum:
  


Current Time: Fri Jan 10 07:32:40 CST 2025