I have a table with employeeid,bossid,salary. I have created a hierarchy of all the direct and indirect managers of every employee.
employeeid | bossid | salary | hierarchy
20 NULL 10000 0
10 20 4500 1
50 10 3000 2
70 10 1500 2
40 50 1500 3
60 70 2000 3
30 50 1501 3
Manager = boss
Indirect manager = direct boss OR Boss of Boss
So for employee 30, boss = 50 and indirect = 50, 10 , 20
I want for each employee to find that employee's lowest ranked indirect boss in the hierarchy who earns at least twice as much as the employee.
This is the query I have used to create the hierarchy. I do not know how to proceed
with recursive EmpMgrCTE
as
(SELECT id,name,boss_id,salary, 0 as EmployeeLevel
from employees a
where boss_id is null
union all
select emp.id,emp.name,emp.boss_id,emp.salary,mgr.EmployeeLevel + 1 as EmployeeLevel
from employees emp
inner join EmpMgrCTE mgr
on emp.boss_id = mgr.id
)
select *
from EmpMgrCTE emp