안녕하세요!
이번 포스팅은 LeetCode에 있는 The Number of Employees Which Report to Each Employee 문제를 OracleDB로 풀어보려고 합니다!
(모든 문제는 Oracle로 풀이하겠습니다.)
1. 문제 링크 : https://leetcode.com/problems/the-number-of-employees-which-report-to-each-employee/description/
2. 문제
| Column name | Type |
| employee_id | int |
| name | varchar |
| reports_to | int |
| age | int |
[문제] 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.
ID와 모든 관리자의 이름, 관리자에게 직접 보고하는 직원의 수, 보고자의 평균 연령을 가장 가까운 정수로 반올림하여 조회하는 문제입니다.
3. 제출 쿼리 및 설명
1) EMPLOYEES 테이블을 셀프 조인합니다. REPORTS_TO의 번호가 EMPLOYEE_ID와 같아야 조인 대상이 됩니다.
SELECT
E1.REPORTS_TO, E1.AGE,
E2.EMPLOYEE_ID, E2.NAME
FROM EMPLOYEES E1, EMPLOYEES E2
WHERE E1.REPORTS_TO = E2.EMPLOYEE_ID
최종) 직원의 ID와 이름에 대해서 보고하는 직원의 수, 보고자의 평균 연령을 반올림해서 정수로 만들어 조회합니다.
SELECT
EMPLOYEE_ID, NAME, COUNT(*) AS REPORTS_COUNT,
ROUND(AVG(AGE), 0) AS average_age
FROM (
SELECT
E1.REPORTS_TO, E1.AGE,
E2.EMPLOYEE_ID, E2.NAME
FROM EMPLOYEES E1, EMPLOYEES E2
WHERE E1.REPORTS_TO = E2.EMPLOYEE_ID
)
GROUP BY EMPLOYEE_ID, NAME
ORDER BY EMPLOYEE_ID
;
'SQL > LeetCode' 카테고리의 다른 글
| [SQL] Primary Department for Each Employee(LeetCode/Oracle) (1) | 2023.10.29 |
|---|---|
| [SQL] Find Total Time Spent by Each Employee(LeetCode/Oracle) (0) | 2023.10.26 |
| [SQL] Daily Leads and Partners(LeetCode/Oracle) (0) | 2023.10.26 |
| [SQL] Invalid Tweets(LeetCode/Oracle) (0) | 2023.10.26 |
| [SQL] Fix Names in a Table(LeetCode/Oracle) (0) | 2023.10.26 |