|
| 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 cte AS ( |
| 28 | + SELECT |
| 29 | + e1.employee_id, |
| 30 | + e1.employee_name, |
| 31 | + e1.manager_id, |
| 32 | + e1.salary, |
| 33 | + 1 AS rank |
| 34 | + FROM Employees AS e1 |
| 35 | + WHERE e1.manager_id IS NULL |
| 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 + 1 AS rank |
| 45 | + FROM Employees AS e2 |
| 46 | + INNER JOIN |
| 47 | + cte AS c |
| 48 | + ON e2.manager_id = c.employee_id |
| 49 | +), |
| 50 | + |
| 51 | +teamsize AS ( |
| 52 | + SELECT |
| 53 | + e3.employee_id AS manager_id, |
| 54 | + e3.employee_id AS member_id, |
| 55 | + e3.salary AS member_salary |
| 56 | + FROM Employees AS e3 |
| 57 | + |
| 58 | + UNION ALL |
| 59 | + |
| 60 | + SELECT |
| 61 | + ts.manager_id, |
| 62 | + e4.employee_id AS member_id, |
| 63 | + e4.salary AS member_salary |
| 64 | + FROM Employees AS e4 |
| 65 | + JOIN --noqa: AM05 |
| 66 | + teamsize AS ts |
| 67 | + ON e4.manager_id = ts.member_id |
| 68 | +) |
| 69 | + |
| 70 | +SELECT |
| 71 | + c.employee_id, |
| 72 | + c.employee_name, |
| 73 | + c.rank AS level, --noqa: RF04 |
| 74 | + COUNT(*) - 1 AS team_size, |
| 75 | + SUM(ts.member_salary) AS budget |
| 76 | +FROM cte AS c |
| 77 | +INNER JOIN |
| 78 | + teamsize AS ts |
| 79 | + ON c.employee_id = ts.manager_id |
| 80 | +GROUP BY c.employee_id, c.employee_name, c.rank |
| 81 | +ORDER BY level ASC, budget DESC, c.employee_name ASC |
0 commit comments