Home » SQL & PL/SQL » SQL & PL/SQL » Hierarchy flattening and repetition
Hierarchy flattening and repetition [message #641563] Tue, 18 August 2015 03:41 Go to next message
loosecontrol
Messages: 3
Registered: December 2011
Location: Bangalore
Junior Member
Trying to flatten parent child relationship to level based hierarchy. considering levels are fixed.

Table data looks like this

child_t parent_t
Neel Null
kamal Neel
kumar kamal

I am trying to convert this to
Level1 Level2 Level3
Neel Neel Neel
Neel Kamal Kamal
Neel Kamal Kumar


So basically, top node should appear at level1 and gets repeated if level2 is null for this, and same with Level3.
But for next row again level1 node should appear, and then level2 if it is not null, and level3 if it is null then level2 should be repeated.

So all nodes should appear in level3.
How to achieve it.

Scripts are here:
create table test_1 (child_t varchar2(10), parent_t varchar2(10));
insert into test_1 values ('NEEL',NULL);
insert into test_1 values ('KAMAL','NEEL');
insert into test_1 values ('KUMAR','KAMAL');
COMMIT;
Re: Hierarchy flattening and repetition [message #641568 is a reply to message #641563] Tue, 18 August 2015 04:51 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

SQL> col level1 format a10
SQL> col level2 format a10
SQL> col level3 format a10
SQL> with
  2    data as (
  3      select sys_connect_by_path(child_t,'/') path,
  4             connect_by_root child_t root, level lvl
  5      from test_1
  6      where level <= 3
  7      connect by prior child_t = parent_t
  8      start with parent_t is null
  9    )
 10  select case lvl
 11           when 1 then substr(path, instr(path,'/',-1,1)+1)
 12           when 2 then substr(path, instr(path,'/',-1,2)+1,
 13                              instr(path,'/',-1,1)-instr(path,'/',-1,2)-1)
 14           else substr(path, instr(path,'/',-1,3)+1,
 15                       instr(path,'/',-1,2)-instr(path,'/',-1,3)-1)
 16         end level1,
 17         case
 18           when lvl in (1,2) then substr(path, instr(path,'/',-1,1)+1)
 19           else substr(path, instr(path,'/',-1,2)+1,
 20                       instr(path,'/',-1,1)-instr(path,'/',-1,2)-1)
 21         end level2,
 22         substr(path, instr(path,'/',-1,1)+1) level3
 23  from data
 24  order by root, lvl
 25  /
LEVEL1     LEVEL2     LEVEL3
---------- ---------- ----------
NEEL       NEEL       NEEL
NEEL       KAMAL      KAMAL
NEEL       KAMAL      KUMAR

Re: Hierarchy flattening and repetition [message #641569 is a reply to message #641568] Tue, 18 August 2015 05:17 Go to previous messageGo to next message
loosecontrol
Messages: 3
Registered: December 2011
Location: Bangalore
Junior Member
Thanks but, i need to achieve it using without start with /connect by. I am trying with left outer joins.

Using start with/connect by, i already have got it. Using regular expression. In a way, same as what you have done. But my requirement is to use ansi standard and not start with,connect by .

Any suggestion?
Re: Hierarchy flattening and repetition [message #641570 is a reply to message #641569] Tue, 18 August 2015 05:23 Go to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

No, I wasted enough time with you.

Previous Topic: Exact String Match in Instr
Next Topic: Challenging Query
Goto Forum:
  


Current Time: Thu Apr 25 14:40:27 CDT 2024