Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8976055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:01:57+00:00 2026-06-15T19:01:57+00:00

I have a table like this: +———+——–+ | EMP_ID | MGR_iD | +———+——–+ |

  • 0

I have a table like this:

+---------+--------+
| EMP_ID  | MGR_iD |
+---------+--------+
|       1 |      1 |
|       2 |      1 |
|       3 |      1 |
|       4 |      2 |
|       5 |      2 |
|       6 |      2 |
|       7 |      3 |
|       8 |      5 |
|       9 |      7 |
|      10 |      5 |
|      11 |      7 |
|      12 |      9 |
|      13 |      9 |
|      14 |      9 |
+---------+--------+

I’m trying to parse this adjacent list to produce a following result set:

| EMP_ID | MGR_ID | LV | LEVEL1 | LEVEL2 | LEVEL3 | LEVEL4 | LEVEL5 |
---------------------------------------------------------------------
|      1 |      1 |  1 |      1 |      1 |      1 |      1 |      1 |
|      2 |      1 |  2 |      1 |      2 |      2 |      2 |      2 |
|      3 |      1 |  2 |      1 |      3 |      3 |      3 |      3 |
|      4 |      2 |  3 |      1 |      2 |      4 |      4 |      4 |
|      5 |      2 |  3 |      1 |      2 |      5 |      5 |      5 |
|      6 |      2 |  3 |      1 |      2 |      6 |      6 |      6 |
|      7 |      3 |  3 |      1 |      3 |      7 |      7 |      7 |
|      8 |      5 |  4 |      1 |      2 |      5 |      8 |      8 |
|      9 |      7 |  4 |      1 |      3 |      7 |      9 |      9 |
|     10 |      5 |  4 |      1 |      2 |      5 |     10 |     10 |
|     11 |      7 |  4 |      1 |      3 |      7 |     11 |     11 |
|     12 |      9 |  5 |      1 |      3 |      7 |      9 |     12 |
|     13 |      9 |  5 |      1 |      3 |      7 |      9 |     13 |
|     14 |      9 |  5 |      1 |      3 |      7 |      9 |     14 |

Here is what I have managed to get so far:

create table PC (
EMP_ID NUMBER NULL,
MGR_ID NUMBER NULL
);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (1.0, 1.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (2.0, 1.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (3.0, 1.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (4.0, 2.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (5.0, 2.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (6.0, 2.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (7.0, 3.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (8.0, 5.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (9.0, 7.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (10.0, 5.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (11.0, 7.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (12.0, 9.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (13.0, 9.0);

INSERT INTO PC (EMP_ID, MGR_ID) 
VALUES (14.0, 9.0);

And the query:

 WITH PERSON_HIER AS 
(
SELECT  1 as level1, 
        CAST(NULL AS NUMBER) as level2,
        CAST(NULL AS NUMBER) as level3,
        CAST(NULL AS NUMBER) as level4,
        CAST(NULL AS NUMBER) as level5
FROM    PC
WHERE EMP_ID = 1 AND MGR_ID = 1
UNION ALL
SELECT  L1.EMP_ID AS LEVEL1,
        L2.EMP_ID AS LEVEL2,
        L3.EMP_ID AS LEVEL3,
        L4.EMP_ID AS LEVEL4,
        L5.EMP_ID AS LEVEL5
FROM PC L1
  LEFT OUTER JOIN PC L2 ON (L1.EMP_ID = L2.MGR_ID AND L2.EMP_ID != L1.EMP_ID)
  LEFT OUTER JOIN PC L3 ON (L2.EMP_ID = L3.MGR_ID)
  LEFT OUTER JOIN PC L4 ON (L3.EMP_ID = L4.MGR_ID)
  LEFT OUTER JOIN PC L5 ON (L4.EMP_ID = L5.MGR_ID)
WHERE L1.MGR_ID = L1.EMP_ID
)
SELECT  level1,
        coalesce(level2, level1) AS LEVEL2,
        coalesce(level3, level2, level1) AS LEVEL3,
        coalesce(level4, level3, level2, level1) AS LEVEL4,
        coalesce(level5, level4, level3, level2, level1) AS LEVEL5              
FROM PERSON_HIER 
order by level5 

I’m using Oracle 10g so Recursive CTE or Hierarchial queries not possible in Oracle 10g

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:01:59+00:00Added an answer on June 15, 2026 at 7:01 pm

    Now an improved version(shows correct hierarcy):

    select emp_id, mgr_id, lvl, h, 
       nvl(substr(h,instr(h,'/',1, 2)+1, instr(h, '/',1, 3)- instr(h,'/',1,2)-1), emp_id) as lvl1,
       nvl(substr(h,instr(h,'/',1, 3)+1, instr(h, '/',1, 4)- instr(h,'/',1,3)-1), emp_id) as lvl2,
       nvl(substr(h,instr(h,'/',1, 4)+1, instr(h, '/',1, 5)- instr(h,'/',1,4)-1), emp_id) as lvl3,
       nvl(substr(h,instr(h,'/',1, 5)+1, instr(h, '/',1, 6) -instr(h,'/',1,5)-1), emp_id) as lvl4,
       nvl(substr(h,instr(h,'/',1, 6)+1, instr(h, '/',1, 7) -instr(h,'/',1,6)-1), emp_id) as lvl5
    
    from(
    select emp_id, mgr_id , level lvl, sys_connect_by_path(mgr_id, '/')||'/' h
    from pc
    connect by nocycle prior emp_id = mgr_id 
    start with emp_id = 1 
    )
    order by emp_id;
    
    EMP_ID  MGR_ID  LVL H               LVL1    LVL2    LVL3    LVL4    LVL5
    2           1   1   1/1/               1    2   2   2   2
    3           1   1   1/1/               1    3   3   3   3
    4           2   2   2/1/2/             1    2   4   4   4
    5           2   2   2/1/2/             1    2   5   5   5
    6           2   2   2/1/2/             1    2   6   6   6
    7           3   2   3/1/3/             1    3   7   7   7
    8           5   3   5/1/2/5/           1    2   5   8   8
    9           7   3   7/1/3/7/           1    3   7   9   9
    10          5   3   5/1/2/5/           1    2   5   10  10
    11          7   3   7/1/3/7/           1    3   7   11  11
    12          9   4   9/1/3/7/9/         1    3   7   9   12
    13          9   4   9/1/3/7/9/         1    3   7   9   13
    14          9   4   9/1/3/7/9/         1    3   7   9   14
    15         14   5   14/1/3/7/9/14/     1    3   7   9   14
    

    SQL FIDDLE

    This is my first try:

    select emp_id, mgr_id, lvl, h,
       nvl(substr(h,instr(h,' ',1, 1), instr(h, ' ',1, 2)- instr(h,' ',1,1)), emp_id) as lvl1,
       nvl(substr(h,instr(h,' ',1, 2), instr(h, ' ',1, 3)- instr(h,' ',1,2)), emp_id) as lvl2,
       nvl(substr(h,instr(h,' ',1, 3), instr(h, ' ',1, 4)- instr(h,' ',1,3)), emp_id) as lvl3,
       nvl(substr(h,instr(h,' ',1, 4), instr(h, ' ',1, 5)- instr(h,' ',1,4)), emp_id) as lvl4,
       nvl(substr(h,instr(h,' ',1, 5), instr(h, ' ',1, 6) -instr(h,' ',1,5)), emp_id) as lvl5
    
    from(
    select emp_id, mgr_id , level lvl, sys_connect_by_path(mgr_id, ' ') h
    from pc
    connect by nocycle prior emp_id = mgr_id 
    start with emp_id = 1 
    )
    order by emp_id;
    

    See SQLFiddle here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table like this: Name Result T1 fail T2 pass T3 pass T2
I have table like this : ID Name_1 Name_2 Name_3 1 Egon Spengler Ives
I have a table like this ID Name IsDeleted 1 Yogesh Null 2 Goldy
I have a table like this : +-------+------------+------+-----+---------+-------+ | Field | Type | Null
I have a table like this: ID country ------------- 1 US 2 Japan 3
I have a table like this: id | id_parent | tag_name 0 | |
I have a table like this: CREATE TABLE book_info ( book_id VARCHAR(32) not null,
I have a table like this: <table> <tr> <td> A-1 </td> <td> A-2 </td>
I have UserComments table like this : 1 | Frank | hello world 2
I have a table like this gems ---------- id, color 1 , green 2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.