|
| 1 | +[](https://github.com/javadev/LeetCode-in-Kotlin) |
| 2 | +[](https://github.com/javadev/LeetCode-in-Kotlin/fork) |
| 3 | + |
| 4 | +## 3482\. Analyze Organization Hierarchy |
| 5 | + |
| 6 | +Hard |
| 7 | + |
| 8 | +Table: `Employees` |
| 9 | + |
| 10 | + +----------------+---------+ |
| 11 | + | Column Name | Type | |
| 12 | + +----------------+---------+ |
| 13 | + | employee_id | int | |
| 14 | + | employee_name | varchar | |
| 15 | + | manager_id | int | |
| 16 | + | salary | int | |
| 17 | + | department | varchar | |
| 18 | + +----------------+---------+ |
| 19 | + employee_id is the unique key for this table. |
| 20 | + Each row contains information about an employee, including their ID, name, their manager's ID, salary, and department. |
| 21 | + manager_id is null for the top-level manager (CEO). |
| 22 | + |
| 23 | +Write a solution to analyze the organizational hierarchy and answer the following: |
| 24 | + |
| 25 | +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). |
| 26 | +2. **Team Size:** For each employee who is a manager, count the total number of employees under them (direct and indirect reports). |
| 27 | +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). |
| 28 | + |
| 29 | +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_._ |
| 30 | + |
| 31 | +_The result format is in the following example._ |
| 32 | + |
| 33 | +**Example:** |
| 34 | + |
| 35 | +**Input:** |
| 36 | + |
| 37 | +Employees table: |
| 38 | + |
| 39 | + +-------------+---------------+------------+--------+-------------+ |
| 40 | + | employee_id | employee_name | manager_id | salary | department | |
| 41 | + +-------------+---------------+------------+--------+-------------+ |
| 42 | + | 1 | Alice | null | 12000 | Executive | |
| 43 | + | 2 | Bob | 1 | 10000 | Sales | |
| 44 | + | 3 | Charlie | 1 | 10000 | Engineering | |
| 45 | + | 4 | David | 2 | 7500 | Sales | |
| 46 | + | 5 | Eva | 2 | 7500 | Sales | |
| 47 | + | 6 | Frank | 3 | 9000 | Engineering | |
| 48 | + | 7 | Grace | 3 | 8500 | Engineering | |
| 49 | + | 8 | Hank | 4 | 6000 | Sales | |
| 50 | + | 9 | Ivy | 6 | 7000 | Engineering | |
| 51 | + | 10 | Judy | 6 | 7000 | Engineering | |
| 52 | + +-------------+---------------+------------+--------+-------------+ |
| 53 | + |
| 54 | +**Output:** |
| 55 | + |
| 56 | + +-------------+---------------+-------+-----------+--------+ |
| 57 | + | employee_id | employee_name | level | team_size | budget | |
| 58 | + +-------------+---------------+-------+-----------+--------+ |
| 59 | + | 1 | Alice | 1 | 9 | 84500 | |
| 60 | + | 3 | Charlie | 2 | 4 | 41500 | |
| 61 | + | 2 | Bob | 2 | 3 | 31000 | |
| 62 | + | 6 | Frank | 3 | 2 | 23000 | |
| 63 | + | 4 | David | 3 | 1 | 13500 | |
| 64 | + | 7 | Grace | 3 | 0 | 8500 | |
| 65 | + | 5 | Eva | 3 | 0 | 7500 | |
| 66 | + | 9 | Ivy | 4 | 0 | 7000 | |
| 67 | + | 10 | Judy | 4 | 0 | 7000 | |
| 68 | + | 8 | Hank | 4 | 0 | 6000 | |
| 69 | + +-------------+---------------+-------+-----------+--------+ |
| 70 | + |
| 71 | +**Explanation:** |
| 72 | + |
| 73 | +* **Organization Structure:** |
| 74 | + * Alice (ID: 1) is the CEO (level 1) with no manager |
| 75 | + * Bob (ID: 2) and Charlie (ID: 3) report directly to Alice (level 2) |
| 76 | + * David (ID: 4), Eva (ID: 5) report to Bob, while Frank (ID: 6) and Grace (ID: 7) report to Charlie (level 3) |
| 77 | + * Hank (ID: 8) reports to David, and Ivy (ID: 9) and Judy (ID: 10) report to Frank (level 4) |
| 78 | +* **Level Calculation:** |
| 79 | + * The CEO (Alice) is at level 1 |
| 80 | + * Each subsequent level of management adds 1 to the level |
| 81 | +* **Team Size Calculation:** |
| 82 | + * Alice has 9 employees under her (the entire company except herself) |
| 83 | + * Bob has 3 employees (David, Eva, and Hank) |
| 84 | + * Charlie has 4 employees (Frank, Grace, Ivy, and Judy) |
| 85 | + * David has 1 employee (Hank) |
| 86 | + * Frank has 2 employees (Ivy and Judy) |
| 87 | + * Eva, Grace, Hank, Ivy, and Judy have no direct reports (team\_size = 0) |
| 88 | +* **Budget Calculation:** |
| 89 | + * Alice's budget: Her salary (12000) + all employees' salaries (72500) = 84500 |
| 90 | + * Charlie's budget: His salary (10000) + Frank's budget (23000) + Grace's salary (8500) = 41500 |
| 91 | + * Bob's budget: His salary (10000) + David's budget (13500) + Eva's salary (7500) = 31000 |
| 92 | + * Frank's budget: His salary (9000) + Ivy's salary (7000) + Judy's salary (7000) = 23000 |
| 93 | + * David's budget: His salary (7500) + Hank's salary (6000) = 13500 |
| 94 | + * Employees with no direct reports have budgets equal to their own salary |
| 95 | + |
| 96 | +**Note:** |
| 97 | + |
| 98 | +* The result is ordered first by level in ascending order |
| 99 | +* Within the same level, employees are ordered by budget in descending order then by name in ascending order |
| 100 | + |
| 101 | +## Solution |
| 102 | + |
| 103 | +```sql |
| 104 | +# Write your MySQL query statement below |
| 105 | +with recursive org_hierarchy(orig_employee_id, orig_employee_name, employee_id, employee_name, manager_id, salary, org_level) as |
| 106 | +( |
| 107 | + select employee_id as orig_employee_id, |
| 108 | + employee_name as orig_employee_name, |
| 109 | + employee_id, |
| 110 | + employee_name, |
| 111 | + manager_id, |
| 112 | + salary, |
| 113 | + 1 as org_level |
| 114 | + from Employees |
| 115 | + UNION ALL |
| 116 | + select P.orig_employee_id, |
| 117 | + P.orig_employee_name, |
| 118 | + CH.employee_id, |
| 119 | + CH.employee_name, |
| 120 | + CH.manager_id, |
| 121 | + CH.salary, |
| 122 | + P.org_level + 1 |
| 123 | + from org_hierarchy P, Employees CH |
| 124 | + where ch.manager_id = P.employee_id |
| 125 | +), |
| 126 | +CEO_hierarchy as ( |
| 127 | + select org_hierarchy.employee_id as SUB_employee_id, |
| 128 | + org_hierarchy.employee_name, |
| 129 | + org_hierarchy.org_level as sub_level |
| 130 | + from org_hierarchy, Employees |
| 131 | + where org_hierarchy.orig_employee_id = Employees.employee_id |
| 132 | + and Employees.manager_id is null |
| 133 | +) |
| 134 | +select |
| 135 | +org_hierarchy.ORIG_EMPLOYEE_ID as employee_id, |
| 136 | +org_hierarchy.ORIG_EMPLOYEE_name as employee_name, |
| 137 | +CEO_hierarchy.sub_level as "level", |
| 138 | +count(*) - 1 as team_size, |
| 139 | +sum(org_hierarchy.salary) as budget |
| 140 | +from org_hierarchy, CEO_hierarchy |
| 141 | +where org_hierarchy.ORIG_EMPLOYEE_ID = CEO_hierarchy.SUB_employee_id |
| 142 | +group by org_hierarchy.ORIG_EMPLOYEE_ID, |
| 143 | +org_hierarchy.ORIG_EMPLOYEE_name, |
| 144 | +CEO_hierarchy.sub_level |
| 145 | +order by 3 asc, 5 desc, 2 |
| 146 | +``` |
0 commit comments