LeetCode-in-Java

3482. Analyze Organization Hierarchy

Hard

Table: Employees

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| employee_id    | int     |
| employee_name  | varchar |
| manager_id     | int     |
| salary         | int     |
| department     | varchar |
+----------------+---------+
employee_id is the unique key for this table.
Each row contains information about an employee, including their ID, name, their manager's ID, salary, and department.
manager_id is null for the top-level manager (CEO). 

Write a solution to analyze the organizational hierarchy and answer the following:

  1. 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).
  2. Team Size: For each employee who is a manager, count the total number of employees under them (direct and indirect reports).
  3. 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).

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._

The result format is in the following example.

Example:

Input:

Employees table:

+-------------+---------------+------------+--------+-------------+
| employee_id | employee_name | manager_id | salary | department  |
+-------------+---------------+------------+--------+-------------+
| 1           | Alice         | null       | 12000  | Executive   |
| 2           | Bob           | 1          | 10000  | Sales       |
| 3           | Charlie       | 1          | 10000  | Engineering |
| 4           | David         | 2          | 7500   | Sales       |
| 5           | Eva           | 2          | 7500   | Sales       |
| 6           | Frank         | 3          | 9000   | Engineering |
| 7           | Grace         | 3          | 8500   | Engineering |
| 8           | Hank          | 4          | 6000   | Sales       |
| 9           | Ivy           | 6          | 7000   | Engineering |
| 10          | Judy          | 6          | 7000   | Engineering |
+-------------+---------------+------------+--------+-------------+ 

Output:

+-------------+---------------+-------+-----------+--------+
| employee_id | employee_name | level | team_size | budget |
+-------------+---------------+-------+-----------+--------+
| 1           | Alice         | 1     | 9         | 84500  |
| 3           | Charlie       | 2     | 4         | 41500  |
| 2           | Bob           | 2     | 3         | 31000  |
| 6           | Frank         | 3     | 2         | 23000  |
| 4           | David         | 3     | 1         | 13500  |
| 7           | Grace         | 3     | 0         | 8500   |
| 5           | Eva           | 3     | 0         | 7500   |
| 9           | Ivy           | 4     | 0         | 7000   |
| 10          | Judy          | 4     | 0         | 7000   |
| 8           | Hank          | 4     | 0         | 6000   |
+-------------+---------------+-------+-----------+--------+ 

Explanation:

Note:

Solution

# Write your MySQL query statement below
with recursive org_hierarchy(orig_employee_id, orig_employee_name, employee_id, employee_name, manager_id, salary, org_level) as
(
    select employee_id as orig_employee_id,
    employee_name as orig_employee_name,
    employee_id,
    employee_name,
    manager_id,
    salary,
    1 as org_level
    from Employees
    UNION ALL
    select P.orig_employee_id,
    P.orig_employee_name,
    CH.employee_id,
    CH.employee_name,
    CH.manager_id,
    CH.salary,
    P.org_level + 1
    from org_hierarchy P, Employees CH
    where ch.manager_id = P.employee_id
),
CEO_hierarchy as (
    select org_hierarchy.employee_id as SUB_employee_id,
    org_hierarchy.employee_name,
    org_hierarchy.org_level as sub_level
    from org_hierarchy, Employees
    where org_hierarchy.orig_employee_id = Employees.employee_id
    and Employees.manager_id is null
)
select
org_hierarchy.ORIG_EMPLOYEE_ID as employee_id,
org_hierarchy.ORIG_EMPLOYEE_name as employee_name,
CEO_hierarchy.sub_level as "level",
count(*) - 1 as team_size,
sum(org_hierarchy.salary) as budget
from org_hierarchy, CEO_hierarchy
where org_hierarchy.ORIG_EMPLOYEE_ID = CEO_hierarchy.SUB_employee_id
group by org_hierarchy.ORIG_EMPLOYEE_ID,
org_hierarchy.ORIG_EMPLOYEE_name,
CEO_hierarchy.sub_level
order by 3 asc, 5 desc, 2