Home » SQL & PL/SQL » SQL & PL/SQL » Herarchical query (Oracle 11g,Linux)
Herarchical query [message #597542] Sat, 05 October 2013 23:15 Go to next message
sspn2010
Messages: 167
Registered: October 2008
Senior Member
Hi,

I've below table with hierarchical data.
with tmp_tbl as
 (select 10 old_id, 20 as new_id from dual
  union
  select 20 old_id, 30 as new_id from dual
  union
  select 30 old_id, 40 as new_id from dual
  union
  select 50 old_id, 60 as new_id from dual
  union
  select 60 old_id, 70 as new_id from dual
)
select * from tmp_tbl;

    OLD_ID     NEW_ID
---------- ----------
        10         20
        20         30
        30         40
        50         60
        60         70



I want the output to be like below. For the given old_id i need latest new_id (check if the new_id present in old_id, loop it until there will not be any entry for new_id in old_id).


    OLD_ID     NEW_ID
---------- ----------
        10         40
        20         40
        30         40
        50         70
        60         70




Tried below query but it's not giving required output. Could someone help me out.


with tmp_tbl as
 (select 10 old_id, 20 as new_id from dual
  union
  select 20 old_id, 30 as new_id from dual
  union
  select 30 old_id, 40 as new_id from dual
  union
  select 50 old_id, 60 as new_id from dual
  union
  select 60 old_id, 70 as new_id from dual
),
final_tbl (old_id, new_id, high_level) as
(select a.old_id, b.new_id, 1
   from tmp_tbl a, tmp_tbl b
  where a.new_id = b.old_id
 union all
 select a.old_id, b.new_id, high_level+1
   from tmp_tbl a, final_tbl b
  where a.old_id = b.old_id
)
cycle old_id set cyc to '1' default '0' 
select * from final_tbl;

    OLD_ID     NEW_ID HIGH_LEVEL CYC
---------- ---------- ---------- ---
        10         30          1 0
        20         40          1 0
        50         70          1 0
        10         30          2 1
        20         40          2 1
        50         70          2 1




Thanks
SM
icon14.gif  Re: Herarchical query [message #597544 is a reply to message #597542] Sun, 06 October 2013 01:03 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
SQL> with tmp_tbl as
  2   (select 10 old_id, 20 as new_id from dual
  3    union
  4    select 20 old_id, 30 as new_id from dual
  5    union
  6    select 30 old_id, 40 as new_id from dual
  7    union
  8    select 50 old_id, 60 as new_id from dual
  9    union
 10    select 60 old_id, 70 as new_id from dual
 11  )
 12  select connect_by_root old_id old_id, new_id
 13  from tmp_tbl
 14  where connect_by_isleaf = 1
 15  connect by prior new_id = old_id
 16  /
    OLD_ID     NEW_ID
---------- ----------
        10         40
        20         40
        30         40
        50         70
        60         70

[Updated on: Sun, 06 October 2013 01:04]

Report message to a moderator

Re: Herarchical query [message #597562 is a reply to message #597544] Sun, 06 October 2013 12:34 Go to previous messageGo to next message
sspn2010
Messages: 167
Registered: October 2008
Senior Member
Thanks a lot Michael! Would you please give me some idea how does it work?
icon1.gif  Re: Herarchical query [message #597563 is a reply to message #597562] Sun, 06 October 2013 12:57 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

SQL Reference, Hierarchical Queries

Re: Herarchical query [message #597564 is a reply to message #597563] Sun, 06 October 2013 13:04 Go to previous message
sspn2010
Messages: 167
Registered: October 2008
Senior Member
Thank you! Very helpful.
Previous Topic: please convert this procedure to oracle
Next Topic: Find the names of all stored proc invoked on execution of the main api
Goto Forum:
  


Current Time: Fri Apr 26 06:51:51 CDT 2024