Oracle SQL Question

Status
Not open for further replies.

OneEyedPete

Wise, Aged Ars Veteran
130
I have a table with an id column and a parent_id column. The parent_id column refers to another rows id column in the same table.

Something like ...

ID | PARENT_ID
---|----------
1 | null
2 | 1
3 | 2

Is there a way with one select statement when given an id to get all the parent ids? So if given 3 it will return 2 and 1?

I hope the question is clear ...

thanks ...
 
question isn't clear.

ID's usually uniquely identify rows in a table, and as such are usually NOT NULL. Just wondering since you have a null in your ParentID column...

So you want something where when you query the table by ID, it returns all the ParentIDs for that ID? Or what?

edit: oh wait, I think I see what you're doing... You have to set up ParentIDs in a separate table, obviously. And you have to ensure that you don't create any cycles. And I don't believe you can create a query that will follow IDs up to the root... Don't have enough experience with Oracle/SQL to answer that one...
 

reboot

Ars Legatus Legionis
10,818
ID's usually uniquely identify rows in a table, and as such are usually NOT NULL. Just wondering since you have a null in your ParentID column...

ID is probably the only key.
Parent_ID null just means it's top level so it's ok.

You have to set up ParentIDs in a separate table, obviously.

Can't do that if you have an unknown # of parents.
recursive tables are a b#tch.
It is usually dealt with externally, with a programming language.
 
I can't see a way. Your data represents a hierarchy, with 1 as the root (hence it has no parent). You want to find out all the ancestors of 3, which means finding 3's parent, which is 2, then 2's parent, which is 1, then 1's parent, which is NULL, so you stop.

That looks to me to be recursive, so I don't imagine there'll be any simple way of finding the information.
 

reboot

Ars Legatus Legionis
10,818
If you have a set # of parents you can use subselects by aliasing the table differently.

let's say 3 is the bottom and you know it it can only have 2 parents :

Code:
select parentid from all_ids id1 where id1.id = 
		(select parentid from all_ids id2 where id2.id = 3
		)  ;

select parentid from all_ids id1 where id1.id = 3 ;


which would return 1 and 2 in two shots.

If you have a way of knowing at what level an ID is you could also generate the number of subselects needed.

Maybe you can work from that ?

If not, you are going to have to go PL/SQL.


Edit: Wow, the "code" tag is gone and you can paste tabs. waaaaay cool.

Edit 2, 3, 4 : too many levels, ids reversed, etc. I need to go to lunch ;)

[This message was edited by reboot on March 20, 2002 at 12:22.]
 

sp1t3

Seniorius Lurkius
20
Dunno if this will help, but it sounds like you're doing a hierarchial query. Try checking out the Oracle doco on this. The example they use is for tree walking through a list of employees and figuring out who is managed by who....

Code:
SELECT LPAD(' ',2*(LEVEL-1)) || ename org_chart, 
        empno, mgr, job
    FROM emp 
    START WITH job = 'PRESIDENT' 
    CONNECT BY PRIOR empno = mgr; 

ORG_CHART    EMPNO      MGR        JOB
------------ ---------- ---------- ---------
KING               7839            PRESIDENT
  JONES            7566       7839 MANAGER
    SCOTT          7788       7566 ANALYST
      ADAMS        7876       7788 CLERK
    FORD           7902       7566 ANALYST
      SMITH        7369       7902 CLERK
  BLAKE            7698       7839 MANAGER
    ALLEN          7499       7698 SALESMAN
    WARD           7521       7698 SALESMAN
    MARTIN         7654       7698 SALESMAN
    TURNER         7844       7698 SALESMAN
      JAMES        7900       7698 CLERK
 CLARK             7782       7839 MANAGER
   MILLER          7934       7782 CLERK
 

Alexandre

Ars Tribunus Militum
1,781
Subscriptor
There is a way to do what you want.

You can use the START WITH and CONNECT BY statments.

It would be something like this:

Code:
select id, parent_id
from   table
start with id = 3
connect by prior id = parent_id

I don't use this much, so there may be something wrong in there (I hadn't time to test it).

Also this way it retrieves each parent_id in a new line:

Code:
ID PARENT_ID
-- ---------
 3         2
 2         1
 1    (null)
 
Status
Not open for further replies.