ALGORITHM

leetcode 1731 The Number of Employees Which Report to Each Employee [MYSQL]

Adev 2024. 5. 10. 23:31

리뷰

  • 셀프조인이 생각 안 나서 오래 고민했다.

 

"the number of employees who report directly to them  by managers" 이 부분이 문제였는데, count(reports_to)로 적으니 group by에 적용돼서 0 출력.

서브쿼리를 사용해서  count(employee_id)를 출력하려고 하니까 이경우엔 group by에 적용시킬 수가 없었다.

 

공부한 것

  • the average age of the reports rounded to the nearest integer. 

반올림

-- CEILING (x)

round(avg(table2.age))

 

문제

더보기

1731. The Number of Employees Which Report to Each Employee
Solved
Easy
Topics
Companies
SQL Schema
Pandas Schema
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 table1.employee_id, table1.name, count(table2.reports_to) as reports_count, 
round(avg(table2.age)) as average_age from Employees as table1 left join Employees as table2 
on table1.employee_id= table2.reports_to group by table1.employee_id having table1.employee_id 
in (select distinct reports_to from Employees where reports_to is not null) 
order by employee_id