-
leetcode - 615. Average Salary: Departments VS CompanySQL 문제풀이 2022. 8. 29. 20:17
난이도 : Hard
Table: Salary
+-------------+------+ | Column Name | Type | +-------------+------+ | id | int | | employee_id | int | | amount | int | | pay_date | date | +-------------+------+ id is the primary key column for this table. Each row of this table indicates the salary of an employee in one month. employee_id is a foreign key from the Employee table.
Table: Employee
+---------------+------+ | Column Name | Type | +---------------+------+ | employee_id | int | | department_id | int | +---------------+------+ employee_id is the primary key column for this table. Each row of this table indicates the department of an employee.
Write an SQL query to report the comparison result (higher/lower/same) of the average salary of employees in a department to the company's average salary.
Return the result table in any order.
The query result format is in the following example.
Example 1:
Input: Salary table: +----+-------------+--------+------------+ | id | employee_id | amount | pay_date | +----+-------------+--------+------------+ | 1 | 1 | 9000 | 2017/03/31 | | 2 | 2 | 6000 | 2017/03/31 | | 3 | 3 | 10000 | 2017/03/31 | | 4 | 1 | 7000 | 2017/02/28 | | 5 | 2 | 6000 | 2017/02/28 | | 6 | 3 | 8000 | 2017/02/28 | +----+-------------+--------+------------+ Employee table: +-------------+---------------+ | employee_id | department_id | +-------------+---------------+ | 1 | 1 | | 2 | 2 | | 3 | 2 | +-------------+---------------+ Output: +-----------+---------------+------------+ | pay_month | department_id | comparison | +-----------+---------------+------------+ | 2017-02 | 1 | same | | 2017-03 | 1 | higher | | 2017-02 | 2 | same | | 2017-03 | 2 | lower | +-----------+---------------+------------+ Explanation: In March, the company's average salary is (9000+6000+10000)/3 = 8333.33... The average salary for department '1' is 9000, which is the salary of employee_id '1' since there is only one employee in this department. So the comparison result is 'higher' since 9000 > 8333.33 obviously. The average salary of department '2' is (6000 + 10000)/2 = 8000, which is the average of employee_id '2' and '3'. So the comparison result is 'lower' since 8000 < 8333.33. With he same formula for the average salary comparison in February, the result is 'same' since both the department '1' and '2' have the same average salary with the company, which is 7000.
SELECT pay_month , department_id , CASE WHEN dpt_avg_salary > avg_salary THEN 'higher' WHEN dpt_avg_salary < avg_salary THEN 'lower' WHEN dpt_avg_salary = avg_salary THEN 'same' END AS comparison FROM ( SELECT s.employee_id , amount , pay_date , department_id , LEFT(pay_date, 7) pay_month , AVG(amount) OVER(PARTITION BY s.pay_date) avg_salary , AVG(amount) OVER(PARTITION BY s.pay_date, e.department_id) dpt_avg_salary FROM Salary s JOIN Employee e ON s.employee_id = e.employee_id) sub GROUP BY 1, 2, 3
Accepted (26.93%)
'SQL 문제풀이' 카테고리의 다른 글
leetcode - 1934. Confirmation Rate (0) 2022.09.02 leetcode - 618. Students Report By Geography (0) 2022.08.29 leetcode - 1841. League Statistics (0) 2022.08.29 leetcode - 1831. Maximum Transaction Each Day (0) 2022.08.29 leetcode - 601. Human Traffic of Stadium (0) 2022.08.26