2

I am looking for solution how to select with use of sys_connect_by_path only full hierarchy 'branches'. I found such example on the Internet, but as result is presented it lacks second query which interests me. Example below. Does anyone know how can i achieve second form of output - full hierarchy skipping shorter ones ?

  SQL>
    SQL> column  full_path format a40
    SQL>
    SQL> select  ename
      2  ,       connect_by_root ename            as Designer
      3  ,       sys_connect_by_path(ename,' > ') as full_path
      4  from    emp
      5  start   with job = 'Designer'
      6  connect by prior empno = mgr;

    ENAME    DESIGNER FULL_PATH
    -------- -------- ----------------------------------------
    Jane     Jane      > Jane
    Smart    Jane      > Jane > Smart
    Ana      Jane      > Jane > Smart > Ana
    Fake     Jane      > Jane > Fake
    Tom      Jane      > Jane > Fake > Tom
    Black    Black     > Black
    Jack     Black     > Black > Jack
    Wil      Black     > Black > Wil
    Mary     Black     > Black > Mary
    Take     Black     > Black > Take
    Jane     Black     > Black > Jane
    Chris    Chris     > Chris
    Mike     Chris     > Chris > Mike
    Peter    Peter     > Peter
    Jane     Peter     > Peter > Jane
    Smart    Peter     > Peter > Jane > Smart
    Ana      Peter     > Peter > Jane > Smart > Ana
    Fake     Peter     > Peter > Jane > Fake
    Tom      Peter     > Peter > Jane > Fake > Tom

    ENAME    DESIGNER FULL_PATH
    -------- -------- ----------------------------------------
    Black    Peter     > Peter > Black
    Jack     Peter     > Peter > Black > Jack
    Wil      Peter     > Peter > Black > Wil
    Mary     Peter     > Peter > Black > Mary
    Take     Peter     > Peter > Black > Take
    Jane     Peter     > Peter > Black > Jane
    Chris    Peter     > Peter > Chris
    Mike     Peter     > Peter > Chris > Mike

1 Answer 1

3

Use the CONNECT_BY_ISLEAF pseudocolumn to filter to only those rows that are at the leaf of the hierarchy tree:

select  ename
,       connect_by_root ename            as Designer
,       sys_connect_by_path(ename,' > ') as full_path
from    emp
WHERE   CONNECT_BY_ISLEAF = 1
start   with job = 'Designer'
connect by prior empno = mgr;
Sign up to request clarification or add additional context in comments.

4 Comments

hi I can accept in 5 minutes and surely will do as it works. Thank you! One more question... can i get rid off of first (parent) node displayed in hierarchy ?
Just take the substring starting from the second item - something like SUBSTR( sys_connect_by_path(ename,' > '), INSTR( ' > ', 1, 2 ) ) - assuming you will always have at least 2 levels to your hierarchy
I am not sure if I missed something but I get it working only when INSTR get the same string as SUBSTR so in that case: (SUBSTR( sys_connect_by_path(ename,' > '), INSTR(sys_connect_by_path(ename,' > '), ' > ', 1, 2 ) ) Now question is if that second occurence of sys_connect_by_path can be replaced with some alias ?
@kolboc My mistake, yes, that is what I had intended to write but just missed out that term.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.