Discussions
Categories
- 196.8K All Categories
- 2.2K Data
- 235 Big Data Appliance
- 1.9K Data Science
- 449.9K Databases
- 221.6K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 549 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 532 SQLcl
- 4K SQL Developer Data Modeler
- 186.9K SQL & PL/SQL
- 21.3K SQL Developer
- 295.5K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.1K Development Tools
- 104 DevOps
- 3.1K QA/Testing
- 645.9K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 154 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 17 Java Essentials
- 158 Java 8 Questions
- 85.9K Java Programming
- 79 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.2K Java SE
- 13.8K Java Security
- 203 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 401 LiveLabs
- 37 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.6K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 230 Portuguese
How to Use Hierarchial Queries or equalent normal queries..

714761
Member Posts: 18
I am using
OS - Windows
Oracle 10g R2
Im new to PL/SQL.
I actually need to find the hierarchy of employees in a company.
For Example..
base employee ->unit mgr ->Team mgr -> Business Analyst -> Delivery Chief.... goes on...
I need to actually find the emp_id of these hierarchy and put them in a table or display it using select statement.
In the above example one of the employee may report to multiple seniors.
For eg.
base employee may report to unit mgr1 unit mgr 2 or team mgr may report to multiple Business analysts.
I want to know how to get these employees hierarchy and put them in a table or just select them for dispalying using select statement.
Only two fields is needed
SELECT emp_id, sr_rep_to_emp
FROM emp_system_role;
emp_id is the employee id and sr_rep_to_emp is the id to whom employee is reporting to.
I dont know how to use Connect by statemts...to perform this.
With Regards
Suvin Prathab
OS - Windows
Oracle 10g R2
Im new to PL/SQL.
I actually need to find the hierarchy of employees in a company.
For Example..
base employee ->unit mgr ->Team mgr -> Business Analyst -> Delivery Chief.... goes on...
I need to actually find the emp_id of these hierarchy and put them in a table or display it using select statement.
In the above example one of the employee may report to multiple seniors.
For eg.
base employee may report to unit mgr1 unit mgr 2 or team mgr may report to multiple Business analysts.
I want to know how to get these employees hierarchy and put them in a table or just select them for dispalying using select statement.
Only two fields is needed
SELECT emp_id, sr_rep_to_emp
FROM emp_system_role;
emp_id is the employee id and sr_rep_to_emp is the id to whom employee is reporting to.
I dont know how to use Connect by statemts...to perform this.
With Regards
Suvin Prathab
Tagged:
Answers
-
You mentioned thatSuvin Prathab wrote:But you did not mention what needs to be displayed in this case.
base employee may report to unit mgr1 unit mgr 2 or team mgr may report to multiple Business analysts.
Regards
Arun -
I actually need to find the hierarchy of employees in a company.you can start with
SELECT LEVEL, emp_id, sr_rep_to_emp FROM emp_system_role CONNECT BY PRIOR emp_id = sr_rep_to_emp START WITH sr_rep_to_emp IS NULL --- assuming the highest level in the organisation has no entry for sr_rep_to_emp
-
Look at the below query
with emp as (
select 1 eno,'a' ename,2 mgr from dual union all
select 2 eno,'b' ename,3 mgr from dual union all
select 3 eno,'c' ename,null mgr from dual
)
select eno,ltrim(sys_connect_by_path((select ename from emp t where t.eno=r.mgr),','),',') from emp r connect by prior eno=mgr start with mgr is null order by r.eno -
Each employee has only one reporting head
emp_id sr_rep_to_emp
---------- ---------------------
a b a reports to b
b c b reports to c
c d c reports to d
d d is the boss
here d is the boss and a is the lower level employee.
Each employee has multiple reporting head
emp_id sr_rep_to_emp
---------- ---------------------
a b a reports to b ----a multiple reporting
a c a reports to c ----a multiple reporting
c d c reports to d
d e d reports to e
e d e reports to d ---- e multiple reporting
e f e reports to f ---- e multiple reporting
f f is the boss
this is wat the result i needed .... -
Each employee has only one reporting head
emp_id------sr_rep_to_emp
-------------------------------------
a---------------------b--------------a reports to b
b---------------------c--------------b reports to c
c---------------------d--------------c reports to d
d---------------------d is the boss
here d is the boss and a is the lower level employee.
Each employee has multiple reporting head
emp_id------sr_rep_to_emp
-------------------------------------
a--------------------- b-------------- a reports to b ----a multiple reporting
a---------------------c--------------a reports to c ----a multiple reporting
c---------------------d-------------- c reports to d
d---------------------e-------------- d reports to e
e---------------------d--------------e reports to d ---- e multiple reporting
e---------------------f --------------e reports to f ---- e multiple reporting
f------------------------------------- f is the boss
this is wat the result i needed .... -
with emp as ( select 1 eno,null mgr from dual union all select 2 eno,1 mgr from dual union all select 3 eno,1 mgr from dual union all select 3 eno,2 mgr from dual ) select eno, eno||' reports to '||mgr FROM EMP CONNECT BY PRIOR ENO=MGR start with mgr=1
1 is manager to 2,3 is reporting to both 1 and 2...check the output
Ravi Kumar
This discussion has been closed.