Christoph's 2 Cents

A Backup for My Brain!

Oracle Developement

Hierarchical Query: Get the ancestors in top down order

This query starts with the leaf node and climbs up the hierarchy to the top ancestor. It then displays the result in the usual top-down order:

CMR@vrep > list
SELECT lvl,
       employee
FROM   (SELECT LEVEL   lvl,
               Lpad(' ', 2 * ( Max(ROWNUM)
                                 over () - LEVEL ), ' ')
               ||empno
               ||', '
               ||ename employee,
               ROWNUM  rn
        FROM   emp
        CONNECT BY empno = PRIOR mgr
        START WITH ename = Upper('&ename'))
ORDER  BY rn DESC 

CMR@vrep > /
Enter value for ename: adams

LVL EMPLOYEE
---------- --------------------------
4 7839, KING
3 7566, JONES
2 7788, SCOTT
1 7876, ADAMS

CMR@vrep >