|
| 1 | +/* |
| 2 | +Question 3482. Analyze Organization Hierarchy |
| 3 | +Link: https://leetcode.com/problems/analyze-organization-hierarchy/description/?envType=problem-list-v2&envId=database |
| 4 | +
|
| 5 | +Table: Employees |
| 6 | +
|
| 7 | ++----------------+---------+ |
| 8 | +| Column Name | Type | |
| 9 | ++----------------+---------+ |
| 10 | +| employee_id | int | |
| 11 | +| employee_name | varchar | |
| 12 | +| manager_id | int | |
| 13 | +| salary | int | |
| 14 | +| department | varchar | |
| 15 | ++----------------+----------+ |
| 16 | +employee_id is the unique key for this table. |
| 17 | +Each row contains information about an employee, including their ID, name, their manager's ID, salary, and department. |
| 18 | +manager_id is null for the top-level manager (CEO). |
| 19 | +Write a solution to analyze the organizational hierarchy and answer the following: |
| 20 | +
|
| 21 | +Hierarchy Levels: For each employee, determine their level in the organization (CEO is level 1, employees reporting directly to the CEO are level 2, and so on). |
| 22 | +Team Size: For each employee who is a manager, count the total number of employees under them (direct and indirect reports). |
| 23 | +Salary Budget: For each manager, calculate the total salary budget they control (sum of salaries of all employees under them, including indirect reports, plus their own salary). |
| 24 | +Return the result table ordered by the result ordered by level in ascending order, then by budget in descending order, and finally by employee_name in ascending order. |
| 25 | +*/ |
| 26 | + |
| 27 | +WITH RECURSIVE cteAS ( |
| 28 | +SELECT |
| 29 | +e1.employee_id, |
| 30 | +e1.employee_name, |
| 31 | +e1.manager_id, |
| 32 | +e1.salary, |
| 33 | +1AS rank |
| 34 | +FROM EmployeesAS e1 |
| 35 | +WHEREe1.manager_id ISNULL |
| 36 | + |
| 37 | +UNION ALL |
| 38 | + |
| 39 | +SELECT |
| 40 | +e2.employee_id, |
| 41 | +e2.employee_name, |
| 42 | +e2.manager_id, |
| 43 | +e2.salary, |
| 44 | +c.rank+1AS rank |
| 45 | +FROM EmployeesAS e2 |
| 46 | +INNER JOIN |
| 47 | + cteAS c |
| 48 | +ONe2.manager_id=c.employee_id |
| 49 | +), |
| 50 | + |
| 51 | +teamsizeAS ( |
| 52 | +SELECT |
| 53 | +e3.employee_idAS manager_id, |
| 54 | +e3.employee_idAS member_id, |
| 55 | +e3.salaryAS member_salary |
| 56 | +FROM EmployeesAS e3 |
| 57 | + |
| 58 | +UNION ALL |
| 59 | + |
| 60 | +SELECT |
| 61 | +ts.manager_id, |
| 62 | +e4.employee_idAS member_id, |
| 63 | +e4.salaryAS member_salary |
| 64 | +FROM EmployeesAS e4 |
| 65 | +JOIN--noqa: AM05 |
| 66 | + teamsizeAS ts |
| 67 | +ONe4.manager_id=ts.member_id |
| 68 | +) |
| 69 | + |
| 70 | +SELECT |
| 71 | +c.employee_id, |
| 72 | +c.employee_name, |
| 73 | +c.rankAS level,--noqa: RF04 |
| 74 | +COUNT(*)-1AS team_size, |
| 75 | +SUM(ts.member_salary)AS budget |
| 76 | +FROM cteAS c |
| 77 | +INNER JOIN |
| 78 | + teamsizeAS ts |
| 79 | +ONc.employee_id=ts.manager_id |
| 80 | +GROUP BYc.employee_id,c.employee_name,c.rank |
| 81 | +ORDER BY levelASC, budgetDESC,c.employee_nameASC |