13.05.2013 Views

Thanks for the question regarding "connect by ", versi

Thanks for the question regarding "connect by ", versi

Thanks for the question regarding "connect by ", versi

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Find Employee's top most manager August 19, 2004 - 6pm Central time zone<br />

Bookmark | Bottom | Top<br />

Reviewer: Mita from NJ<br />

For <strong>the</strong> following data,<br />

Emp_id Mgr_id<br />

1 2<br />

2 3<br />

3 4<br />

4<br />

5 6<br />

6 7<br />

7<br />

I need <strong>the</strong> following result<br />

EmpId TopManager<br />

1 4<br />

2 4<br />

3 4<br />

4 4<br />

5 7<br />

6 7<br />

7 7<br />

How can I achieve this ??<br />

Followup August 19, 2004 - 8pm Central time zone:<br />

if you gave me a create table and insert into's, i'd show you how to use a scalar subquery with<br />

<strong>connect</strong> <strong>by</strong> using max to do this.<br />

maybe that is enough to get you going....<br />

Sample Data August 24, 2004 - 12pm Central time zone Bookmark | Bottom | Top<br />

Reviewer: Mita from NJ<br />

create table emp (emp_Id number, mgr_Id number);<br />

insert into emp values(1,2);<br />

insert into emp values(2,3);<br />

insert into emp values(3,4);<br />

insert into emp values(4,Null);<br />

insert into emp values(5,6);<br />

insert into emp values(6,7);<br />

insert into emp values(7,Null);<br />

Followup August 24, 2004 - 3pm Central time zone:<br />

ops$tkyte@ORA9IR2> select emp_id,<br />

2 to_number( substr(<br />

3 (select max( to_char(level,'fm000009') || ' ' || emp_id )<br />

4 from emp e2<br />

5 start with e2.emp_id = e1.emp_id<br />

6 <strong>connect</strong> <strong>by</strong> prior mgr_id is not null and prior mgr_id = emp_id )<br />

7 , 8 ) ) top_mgr<br />

8 from emp e1<br />

9 /<br />

EMP_ID TOP_MGR<br />

---------- ----------<br />

1 4<br />

2 4

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!