Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit058afd5

Browse files
committed
task: #3482 CHALLENGE COMPLETED (all SQL freemium tasks have solutions)
1 parentcbc0991 commit058afd5

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ Useful for preparing for technical interviews and improving your SQL skills.
199199
-[601. Human Traffic of Stadium](./leetcode/hard/601.%20Human%20Traffic%20of%20Stadium.sql)
200200
-[3374. First Letter Capitalization II](./leetcode/hard/3374.%20First%20Letter%20Capitalization%20II.sql)
201201
-[3451. Find Invalid IP Addresses](./leetcode/hard/3451.%20Find%20Invalid%20IP%20Addresses.sql)
202+
-[3482. Analyze Organization Hierarchy](./leetcode/hard/3482.%20Analyze%20Organization%20Hierarchy.sql)
202203
-[3554. Find Category Recommendation Pairs](./leetcode/hard/3554.%20Find%20Category%20Recommendation%20Pairs.sql)
203204

204205
##Contributing
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp