Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with
- hierarchical_table ( entity_code, entity_name, entity_role, parent_entity_code ) as (
- select 100, 'Mack' , 'Manager', cast (null as number) from dual union all
- select 200, 'Shail', 'Team-Lead', 100 from dual union all
- select 300, 'Jack' , 'Team-Lead', 100 from dual union all
- select 400, 'Teju' , 'Developer', 200 from dual union all
- select 500, 'Neha' , 'Developer', 200 from dual union all
- select 600, 'Rocky', 'Developer', 300 from dual
- ),
- client_table ( entity_code, client_name, address ) as (
- select 600, 'Voda' , 'Pune' from dual union all
- select 600, 'Rel' , 'Mumbai' from dual union all
- select 600, 'Airtel', 'Pune' from dual union all
- select 500, 'Tata' , 'Mumbai' from dual
- )
- -- end of test data (not part of the solution)
- -- SQL query begins BELOW THIS LINE; use your actual table names
- select h1.entity_code as manager_code, h1.entity_name as manager_name,
- h2.entity_code as teamlead_code, h2.entity_name as teamlead_name,
- h3.entity_code as developer_code, h3.entity_name as developer_name,
- c.client_name
- from hierarchical_table h1 left join hierarchical_table h2
- on h2.parent_entity_code = h1.entity_code
- left join hierarchical_table h3
- on h3.parent_entity_code = h2.entity_code
- left join client_table c
- on c.entity_code = h3.entity_code
- where h1.parent_entity_code is null
- order by manager_code, teamlead_code, developer_code, client_name
- ;
- MANAGER_CODE MANAGER_NAME TEAMLEAD_CODE TEAMLEAD_NAME DEVELOPER_CODE DEVELOPER_NAME CLIENT
- ------------ ------------ ------------- ------------- -------------- -------------- ------
- 100 Mack 200 Shail 400 Teju
- 100 Mack 200 Shail 500 Neha Tata
- 100 Mack 300 Jack 600 Rocky Airtel
- 100 Mack 300 Jack 600 Rocky Rel
- 100 Mack 300 Jack 600 Rocky Voda
- 5 rows selected.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement