I wrote a Oracle Hierarchical query which will give us Top managers of Particular Employee.
For Example If we have sample Emp and Manager mapping like :
WITH emp_manager_mapping AS (
select 'A' empId, 'B' managerId,sysdate-100 appliedOn,'Success' status from dual
union all
select 'C' empId, 'D' managerId, sysdate-70 appliedOn, 'Success' status from dual
union all
select 'B' empId, 'C' managerId, sysdate-50 appliedOn,'Success' status from dual
)select * from emp_manager_mapping;
Output is :
A B 10-SEP-19 Success
C D 10-OCT-19 Success
B C 30-OCT-19 Success
After that we applied hierarchical query on this data set what to find who is top manager of Employee id "A" than:
WITH emp_manager_mapping AS (
select 'A' empId, 'B' managerId,sysdate-100 appliedOn,'Success' status from dual
union all
select 'C' empId, 'D' managerId, sysdate-70 appliedOn, 'Success' status from dual
union all
select 'B' empId, 'C' managerId, sysdate-50 appliedOn,' Success' status from dual
) SELECT
CONNECT_BY_ROOT ( empid ) AS empid,
CONNECT_BY_ROOT ( managerid ) AS managerid,
managerid AS top_manager_id,
level
FROM
emp_manager_mapping
WHERE
CONNECT_BY_ISLEAF = 1 and status = 'Success'
START WITH
empid = 'A'
CONNECT BY NOCYCLE
PRIOR managerid = empid;
Than output is:
Value of top_manager_id is D
As per the query it is providing query but not with applied date if we consider the latest applied date also than we have to ignore
C D 10-OCT-19 Success record.
and i want the final output top_manager id should be "C"
Can any one is helping to find expected result?