Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. with
  2. hierarchical_table ( entity_code, entity_name, entity_role, parent_entity_code ) as (
  3. select 100, 'Mack' , 'Manager', cast (null as number) from dual union all
  4. select 200, 'Shail', 'Team-Lead', 100 from dual union all
  5. select 300, 'Jack' , 'Team-Lead', 100 from dual union all
  6. select 400, 'Teju' , 'Developer', 200 from dual union all
  7. select 500, 'Neha' , 'Developer', 200 from dual union all
  8. select 600, 'Rocky', 'Developer', 300 from dual
  9. ),
  10. client_table ( entity_code, client_name, address ) as (
  11. select 600, 'Voda' , 'Pune' from dual union all
  12. select 600, 'Rel' , 'Mumbai' from dual union all
  13. select 600, 'Airtel', 'Pune' from dual union all
  14. select 500, 'Tata' , 'Mumbai' from dual
  15. )
  16. -- end of test data (not part of the solution)
  17. -- SQL query begins BELOW THIS LINE; use your actual table names
  18. select h1.entity_code as manager_code, h1.entity_name as manager_name,
  19. h2.entity_code as teamlead_code, h2.entity_name as teamlead_name,
  20. h3.entity_code as developer_code, h3.entity_name as developer_name,
  21. c.client_name
  22. from hierarchical_table h1 left join hierarchical_table h2
  23. on h2.parent_entity_code = h1.entity_code
  24. left join hierarchical_table h3
  25. on h3.parent_entity_code = h2.entity_code
  26. left join client_table c
  27. on c.entity_code = h3.entity_code
  28. where h1.parent_entity_code is null
  29. order by manager_code, teamlead_code, developer_code, client_name
  30. ;
  31.  
  32. MANAGER_CODE MANAGER_NAME TEAMLEAD_CODE TEAMLEAD_NAME DEVELOPER_CODE DEVELOPER_NAME CLIENT
  33. ------------ ------------ ------------- ------------- -------------- -------------- ------
  34. 100 Mack 200 Shail 400 Teju
  35. 100 Mack 200 Shail 500 Neha Tata
  36. 100 Mack 300 Jack 600 Rocky Airtel
  37. 100 Mack 300 Jack 600 Rocky Rel
  38. 100 Mack 300 Jack 600 Rocky Voda
  39.  
  40. 5 rows selected.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement