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!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Hierarchical query with each level displayed (level 1, level2, level 3) for each row

RajeshAlexFeb 20 2025 — edited Feb 20 2025

Hi All,

My requirement is to get all the level of children in each row.

For eg

Above is from the standard emp table.

KING is the first level

CLARK BLAKE JONES second level

JAMES WARD TURNER etc third level

My requirement is to display all levels at each row in a seperate column.

The closest query I wrote is below.

SELECT level1,
level2,
level3,
level4,
level5,
level6,
level7,
level8,
empno
FROM ( SELECT LEVEL AS n,
ename AS id,
empno,
ename
FROM (SELECT ename, mgr, empno FROM emp) t
START WITH mgr IS NULL
CONNECT BY PRIOR empno = mgr)
PIVOT (
MAX (ename)
FOR n
IN (1 AS level1,
2 AS level2,
3 AS level3,
4 AS level4,
5 AS level5,
6 AS level6,
7 AS level7,
8 AS level8)) ;

However it shows the data as below

Thank you

Best Regards

Alex

This post has been answered by Frank Kulash on Feb 20 2025
Jump to Answer
Comments
Post Details
Added on Feb 20 2025
8 comments
278 views