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.

7902 ******FORD 7566 3000<br />

as you can see , <strong>the</strong> condition returns King, Scott and Ford . but Jones in <strong>the</strong> list because he is<br />

<strong>the</strong> MGR of Scott and Ford.<br />

That what I want. How can I do this.<br />

<strong>Thanks</strong><br />

Followup May 8, 2006 - 7am Central time zone:<br />

that is a bit of a sticky problem. we can do it upside down ra<strong>the</strong>r easily:<br />

ops$tkyte@ORA10GR2> select EMPNO,SUBSTR(rpad('*',2*level,'*' ) || ENAME,1,30), MGR,SAL<br />

2 FROM EMP<br />

3 start with sal >= 3000<br />

4 CONNECT BY PRIOR mgr = empno<br />

5 /<br />

EMPNO SUBSTR(RPAD('*',2*LEVEL,'*')|| MGR SAL<br />

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

7788 **SCOTT 7566 3000<br />

7566 ****JONES 7839 2975<br />

7839 ******KING 5000<br />

7839 **KING 5000<br />

7902 **FORD 7566 3000<br />

7566 ****JONES 7839 2975<br />

7839 ******KING 5000<br />

7 rows selected.<br />

but going from <strong>the</strong> top down is a tad harder:<br />

ops$tkyte@ORA10GR2> select EMPNO,SUBSTR(rpad('*',2*level,'*' ) || ENAME,1,30), MGR,SAL<br />

2 FROM EMP<br />

3 where empno in ( select EMPNO<br />

4 FROM EMP<br />

5 start with sal >= 3000<br />

6 CONNECT BY PRIOR mgr = empno )<br />

7 start with empno in ( select EMPNO<br />

8 FROM EMP<br />

9 where <strong>connect</strong>_<strong>by</strong>_isleaf = 1<br />

10 start with sal >= 3000<br />

11 <strong>connect</strong> <strong>by</strong> prior mgr = empno )<br />

12 CONNECT BY PRIOR empno = mgr<br />

13 /<br />

EMPNO SUBSTR(RPAD('*',2*LEVEL,'*')|| MGR SAL<br />

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

7839 **KING 5000<br />

7566 ****JONES 7839 2975<br />

7788 ******SCOTT 7566 3000<br />

7902 ******FORD 7566 3000<br />

it would only make sense to do <strong>the</strong> "start with" subquery IF you have multiple roots and thought<br />

that <strong>the</strong> filtering done <strong>by</strong> <strong>the</strong> subquery would prune away large parts of <strong>the</strong> hierarchy - o<strong>the</strong>rwise,<br />

just build <strong>the</strong> entire hierarchy and <strong>the</strong>n prune it with <strong>the</strong> where clause.<br />

May 9, 2006 - 4pm Central time zone Bookmark | Bottom | Top<br />

Reviewer: Isam from Jordan<br />

Thank you <strong>for</strong> help.<br />

That is what I need. I will use this logic in different queries of my project.<br />

<strong>Thanks</strong> again.<br />

Connect <strong>by</strong> doing extra full Scan? June 1, 2006 - 12pm Central time zone Bookmark | Bottom | Top<br />

Reviewer: Greg from Toronto

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

Saved successfully!

Ooh no, something went wrong!