forked from DataStudySquad/SQL-Leetcode-Challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Average Salary.sql
65 lines (43 loc) · 2.28 KB
/
Average Salary.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
-- Question 108
-- Given two tables as below, write a query to display the comparison result (higher/lower/same) of the
-- average salary of employees in a department to the company's average salary.
-- Table: salary
-- | 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 |
-- The employee_id column refers to the employee_id in the following table employee.
-- | employee_id | department_id |
-- |-------------|---------------|
-- | 1 | 1 |
-- | 2 | 2 |
-- | 3 | 2 |
-- So for the sample data above, the result is:
-- | pay_month | department_id | comparison |
-- |-----------|---------------|-------------|
-- | 2017-03 | 1 | higher |
-- | 2017-03 | 2 | lower |
-- | 2017-02 | 1 | same |
-- | 2017-02 | 2 | same |
-- 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.
-- Solution
with t1 as(
select date_format(pay_date,'%Y-%m') as pay_month, department_id, avg(amount) over(partition by month(pay_date),department_id) as dept_avg,
avg(amount) over(partition by month(pay_date)) as comp_avg
from salary s join employee e
using (employee_id))
select distinct pay_month, department_id,
case when dept_avg>comp_avg then "higher"
when dept_avg = comp_avg then "same"
else "lower"
end as comparison
from t1
order by 1 desc