Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Parent Child Query

424110Nov 4 2010 — edited Nov 4 2010
i got the output for parent child relation

select
lpad(' ', 2*level) || child
from
prune_test
start with
parent is null
connect by
prior child=parent

like
1
----12
---- 120
-----124
-----127
-----712
-------7122
-------7125
--------7127

But I want out like
Parent Child1 Child 2 child3
1
1 12
1 12 120
1 12 124
1 12 127
1 12 712
1 12 712 7122
1 12 712 7125
1 12 712 7127

Can any one help me...
Thanks
mani

Edited by: Mani on Nov 4, 2010 6:54 PM
This post has been answered by pollywog on Nov 4 2010
Jump to Answer

Comments

pollywog
Answer
think you just need a sys connect by path
with prune_test as 
(select null parent, 1 child from dual union all
 select 1, 12 from dual union all
 select 12, 120 from dual union all 
  select 12, 124 from dual union all
   select 12, 127 from dual union all
   select 12, 712 from dual union all
   select 712,71522 from dual union all
   select 712, 7125 from dual union all
   select 712, 7127 from dual
   )

select
lpad(' ', 2*level) || child, sys_connect_by_path(child,' ')
from
prune_test
start with
parent is null
connect by
prior child=parent 
LPAD('',2*LEVEL)||CHILD	SYS_CONNECT_BY_PATH(CHILD,'')
  1		 1
    12	 	 1 12
      120	 1 12 120
      124	 1 12 124
      127	 1 12 127
      712	 1 12 712
        71522	 1 12 712 71522
        7125	 1 12 712 7125
        7127	 1 12 712 7127
Marked as Answer by 424110 · Sep 27 2020
Frank Kulash
Hi,

So you want a different column for every level, is that right?
In Oracle 9, you can get all the data together into a delimited list using SYS_CONNECT_BY_PATH.
In Oracle 10, you cn use REGEXP_SUBSTR to pick out just the n-th item from that list. (In Oracle 9 you have to use INSTR and SUBSTR, which is more tedious.)

You didn't post CREATE TABLE and INSERT statements for your data, so I'll use the scott.emp table to illustrate:
WITH	got_path	AS
(
	SELECT	SYS_CONNECT_BY_PATH (ename, ',')	AS path
	,	ROWNUM		    	    		AS r_num
	FROM	scott.emp
	START WITH	mgr	IS NULL
	CONNECT BY	mgr	= PRIOR empno
)
SELECT	  REGEXP_SUBSTR (path, '[^,]+', 1, 1)	AS parent
,	  REGEXP_SUBSTR (path, '[^,]+', 1, 2)	AS child_1
,	  REGEXP_SUBSTR (path, '[^,]+', 1, 3)	AS child_2
FROM	  got_path
ORDER BY  r_num
;
Output:
PARENT     CHILD_1    CHILD_2
---------- ---------- ----------
KING
KING       JONES
KING       JONES      SCOTT
KING       JONES      SCOTT
KING       JONES      FORD
KING       JONES      FORD
KING       BLAKE
KING       BLAKE      ALLEN
KING       BLAKE      WARD
KING       BLAKE      MARTIN
KING       BLAKE      TURNER
KING       BLAKE      JAMES
KING       CLARK
KING       CLARK      MILLER
I delibnerately left out the child_3 column to show what happens when you have more levels than columns: the last columns simply don't appear. (SCOTT and FORD, above, have one child each.)
Of course, you can have as many columns as you like, but they have to be hard-coded into the query (or you have to use dynamic SQL to do that for you).

Things would be a lot simpler if you didn't need separate columns. You could use just SYS_CONNECT_BY_PATH to form one big string, formatted so that it looked like separate columns.
Aketi Jyuuzou
I like recursive with clause B-)
with rec(empno,mgr,PARENT,CHILD_1,CHILD_2,LV) as(
select empno,mgr,ename,null,null,1
FROM scott.emp
where mgr   IS NULL
union all
select b.empno,b.mgr,a.PARENT,
case LV+1 when 2 then b.ename else a.CHILD_1 end,
case LV+1 when 3 then b.ename else a.CHILD_2 end,a.LV+1
  from rec a,scott.emp b
 where a.empno = b.mgr)
search depth first by empno set SortKey
select * from rec order by SortKey;

EMPNO   MGR  PARENT  CHILD_1  CHILD_2  LV  SORTKEY
-----  ----  ------  -------  -------  --  -------
 7839  null  KING    null     null      1        1
 7566  7839  KING    JONES    null      2        2
 7788  7566  KING    JONES    SCOTT     3        3
 7876  7788  KING    JONES    SCOTT     4        4
 7902  7566  KING    JONES    FORD      3        5
 7369  7902  KING    JONES    FORD      4        6
 7698  7839  KING    BLAKE    null      2        7
 7499  7698  KING    BLAKE    ALLEN     3        8
 7521  7698  KING    BLAKE    WARD      3        9
 7654  7698  KING    BLAKE    MARTIN    3       10
 7844  7698  KING    BLAKE    TURNER    3       11
 7900  7698  KING    BLAKE    JAMES     3       12
 7782  7839  KING    CLARK    null      2       13
 7934  7782  KING    CLARK    MILLER    3       14
1 - 3
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Dec 2 2010
Added on Nov 4 2010
3 comments
1,471 views