The Number of Employees Which Report to Each Employee
문제 링크
https://leetcode.com/problems/the-number-of-employees-which-report-to-each-employee/description/
문제 설명
Table: Employees
+-------------+----------+
| Column Name | Type |
+-------------+----------+
| employee_id | int |
| name | varchar |
| reports_to | int |
| age | int |
+-------------+----------+
employee_id is the column with unique values for this table.
This table contains information about the employees and the id of the manager they report to. Some employees do not report to anyone (reports_to is null).
For this problem, we will consider a manager an employee who has at least 1 other employee reporting to them.
Write a solution to report the ids and the names of all managers, the number of employees who report directly to them, and the average age of the reports rounded to the nearest integer.
Return the result table ordered by employee_id.
The result format is in the following example.
Example 1:
Input:
Employees table:
+-------------+---------+------------+-----+
| employee_id | name | reports_to | age |
+-------------+---------+------------+-----+
| 9 | Hercy | null | 43 |
| 6 | Alice | 9 | 41 |
| 4 | Bob | 9 | 36 |
| 2 | Winston | null | 37 |
+-------------+---------+------------+-----+
Output:
+-------------+-------+---------------+-------------+
| employee_id | name | reports_count | average_age |
+-------------+-------+---------------+-------------+
| 9 | Hercy | 2 | 39 |
+-------------+-------+---------------+-------------+
Explanation: Hercy has 2 people report directly to him, Alice and Bob. Their average age is (41+36)/2 = 38.5, which is 39 after rounding it to the nearest integer.
Example 2:
Input:
Employees table:
+-------------+---------+------------+-----+
| employee_id | name | reports_to | age |
|-------------|---------|------------|-----|
| 1 | Michael | null | 45 |
| 2 | Alice | 1 | 38 |
| 3 | Bob | 1 | 42 |
| 4 | Charlie | 2 | 34 |
| 5 | David | 2 | 40 |
| 6 | Eve | 3 | 37 |
| 7 | Frank | null | 50 |
| 8 | Grace | null | 48 |
+-------------+---------+------------+-----+
Output:
+-------------+---------+---------------+-------------+
| employee_id | name | reports_count | average_age |
| ----------- | ------- | ------------- | ----------- |
| 1 | Michael | 2 | 40 |
| 2 | Alice | 2 | 37 |
| 3 | Bob | 1 | 37 |
+-------------+---------+---------------+-------------+
SELECT e1.reports_to employee_id,
(SELECT name FROM Employees WHERE employee_id = e1.reports_to ) name,
COUNT(e1.reports_to) reports_count, ROUND(AVG(e1.age)) average_age
FROM Employees e1
GROUP BY e1.reports_to
HAVING e1.reports_to IS NOT NULL
ORDER BY 1
Scalar Subquery 를 제외한 나머지 문장들이 메인 테이블 안에서 바로 도출될 수 있어서
풀다 보니 이런 모습이 되었다. 하지만 제출 시 성능이 좋지 않아 JOIN 을 사용해 다시 풀어보기로 했다.
SELECT e2.employee_id, e2.name, e1.cnt reports_count, e1.age average_age
FROM ( SELECT reports_to, COUNT(reports_to) cnt, ROUND(AVG(age)) age
FROM Employees
WHERE reports_to IS NOT NULL
GROUP BY reports_to) e1
JOIN ( SELECT employee_id, name FROM Employees) e2
ON e1.reports_to = e2.employee_id
ORDER BY 1
조금은 성능이 올라갔지만 미비한 결과였다.
select a.employee_id,a.name,
count(*) reports_count,
round(avg(b.age)) average_age
from employees a join employees b on a.employee_id=b.reports_to
group by a.employee_id
order by a.employee_id
상위 코드 구경하러 갔는데
세상에 역시나.. 내가 생각이 복잡했던 걸까
이렇게 간단하게 구현할 수 있다는 게 신기하다.