Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Hierarchical query based on a leaf node condition

Matt MulvaneyMay 18 2015 — edited May 19 2015

Hi,

I would like to perform a hierarchical query based on a leaf node condition for example; in the below query/results, If the leaf node name starts  in 'S' I want to remove this leaf-node and remove the parent providing it doesn't have any other leaf nodes that don't start with 'S', and apply the same criteria up the chain..

In the below example, SMITH starts with S, so I want to remove it, and then remove FORD because it has no other non-'S' child records. SCOTT stays, because its not a leaf node and therefore ADAMS stays too

SELECT empno, LPAD(' ', LEVEL * 2) ||  ename ename, mgr

   FROM emp

   START WITH empno  = 7839

   CONNECT BY PRIOR empno = mgr;

17839  KING
27566    JONES7839
37788      SCOTT7566
47876        ADAMS7788
57902      FORD7566
67369        SMITH7902
77698    BLAKE7839
87499      ALLEN7698
97521      WARD7698
107654      MARTIN7698
117844      TURNER7698
127900      JAMES7698
137782    CLARK7839
147934      MILLER7782
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 16 2015
Added on May 18 2015
5 comments
3,685 views