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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

-Peter<br />

Can I get <strong>the</strong> parent position w.r.t. to <strong>the</strong> rownum? March 5, 2004 - 4pm Central time zone<br />

Bookmark | Bottom | Top<br />

Reviewer: Peter Tran from Houston, TX USA<br />

Hi Tom,<br />

Is it possible <strong>for</strong> me to generate <strong>the</strong> ParentPosition index w.r.t. to <strong>the</strong> assigned ROWNUM? Of<br />

course, ROWNUM starts at 1, but my example below is using base 0. Ei<strong>the</strong>r way, you can see where<br />

I'm getting at with <strong>the</strong> example below.<br />

Currently, I'm doing this mapping in code. It would be much better if I can do this within SQL.<br />

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

-Peter<br />

ROWNUM ENAME EMPNO MGR ParentPos<br />

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

0 KING 7839 7839 0<br />

1 JONES 7566 7839 0<br />

2 FORD 7902 7566 1<br />

3 SMITH 7369 7902 2<br />

4 SCOTT 7788 7566 1<br />

5 ADAMS 7876 7788 4<br />

6 BLAKE 7698 7839 0<br />

7 ALLEN 7499 7698 6<br />

8 WARD 7521 7698 6<br />

9 MARTIN 7654 7698 6<br />

10 TURNER 7844 7698 6<br />

11 JAMES 7900 7698 6<br />

12 CLARK 7782 7839 0<br />

13 MILLER 7934 7782 12<br />

Followup March 5, 2004 - 7pm Central time zone:<br />

what is "parentPos"<br />

ParentPos March 6, 2004 - 5pm Central time zone Bookmark | Bottom | Top<br />

Reviewer: Peter Tran from Houston, TX USA<br />

ParentPos is <strong>the</strong> RowNum value of <strong>the</strong> parent.<br />

King is <strong>the</strong> parent of Jones, Blake, and Clark which is why ParentPos <strong>for</strong> <strong>the</strong>m is 0.<br />

Blake is <strong>the</strong> parent of Alan, Ward, Martin, Turner, and James, so you see <strong>the</strong>ir ParentPos is 6<br />

because Blake's RowNum is 6.<br />

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

-Peter<br />

Followup March 6, 2004 - 8pm Central time zone:<br />

scott@ORA9IR2> select rnum, ename, empno, mgr,<br />

2 substr( scbp2, 1, instr(scbp2,',')-1 ) parentpos<br />

3 from (<br />

4 select a.*,<br />

5 substr( scbp, instr(scbp, ',', -1, 2 )+1 ) scbp2<br />

6 from (<br />

7 select rownum-1 rnum, rpad( ' ', 2*level, ' ' ) || ename ename, empno, mgr,<br />

8 sys_<strong>connect</strong>_<strong>by</strong>_path( rownum-1, ',' ) scbp<br />

9 from emp<br />

10 start with mgr is null<br />

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

12 ) a

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

Saved successfully!

Ooh no, something went wrong!