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.

Followup October 15, 2004 - 11am Central time zone:<br />

do you have to live with this "structure"? ugh. it hurts my head to look at it. <strong>the</strong> names don't<br />

even make sense.<br />

Ugh October 15, 2004 - 2pm Central time zone Bookmark | Bottom | Top<br />

Reviewer: Vinnie from Orlando<br />

Ugh is right, I have to live with <strong>the</strong> structure!<br />

Perhaps I can explain this better.<br />

I have table EMP with a list of emplyees, and table SUB with each employees subordinates. What I<br />

need is a list of all <strong>the</strong> subordinates <strong>for</strong> a given EMP rolled up to include all subordinate<br />

emplyees. So if I select employee SMITH I get:<br />

SMITH<br />

ADAMS<br />

JONES<br />

ARMSTRONG<br />

FRANK<br />

JAMES<br />

BROWN<br />

But The structure is still <strong>the</strong> same as described be<strong>for</strong>e.<br />

CREATE TABLE EMP (row_id number, ename varchar2(30));<br />

CREATE TABLE SUB (row_id_parent number, ename varchar2(30));<br />

Just assume <strong>the</strong> names are unique <strong>for</strong> this test case.<br />

INSERT INTO EMP (1,'SMITH');<br />

INSERT INTO EMP (2,'ADAMS');<br />

INSERT INTO EMP (3,'JONES');<br />

INSERT INTO EMP (4,'ARMSTRONG');<br />

INSERT INTO EMP (5,'FRANK');<br />

INSERT INTO EMP (6,'JAMES');<br />

INSERT INTO EMP (7,'BROWN');<br />

INSERT INTO SUB (1,'ADAMS');<br />

INSERT INTO SUB (1,'BROWN');<br />

INSERT INTO SUB (2,'JONES');<br />

INSERT INTO SUB (2,'ARMSTONG');<br />

INSERT INTO SUB (2,'JAMES');<br />

INSERT INTO SUB (4,'FRANK');<br />

Hope this helps with your head ache:)<br />

Followup October 15, 2004 - 5pm Central time zone:<br />

ops$tkyte@ORA9IR2> select rpad('*',2*level,'*') || ename name<br />

2 from (select row_id_parent, ename, (select row_id from emp where ename = sub.ename) row_id<br />

from sub)<br />

3 start with row_id_parent = ( select row_id from emp where ename = 'SMITH' )<br />

4 <strong>connect</strong> <strong>by</strong> prior row_id = row_id_parent<br />

5 /<br />

NAME<br />

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

**ADAMS<br />

****JONES<br />

****ARMSTRONG<br />

******FRANK<br />

****JAMES<br />

**BROWN<br />

6 rows selected.<br />

I figure "smith" can be "implied", you could union him in, but you already sort of know "smith"

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

Saved successfully!

Ooh no, something went wrong!