Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7631eae

Browse files
add 615
1 parent90cf3be commit7631eae

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ Your ideas/fixes/algorithms are more than welcome!
625625
|620|[Not Boring Movies](https://leetcode.com/problems/not-boring-movies/)|[Solution](../master/database/_620.sql)||| Easy|
626626
|619|[Biggest Single Number](https://leetcode.com/problems/biggest-single-number/)|[Solution](../master/database/_619.sql)||| Easy|
627627
|618|[Students Report By Geography](https://leetcode.com/problems/students-report-by-geography/)|[Solution](../master/database/_618.sql) | | | Hard | Session Variables
628+
|615|[Average Salary: Departments VS Company](https://leetcode.com/problems/average-salary-departments-vs-company/)|[Solution](../master/database/_615.sql)||| Hard|
628629
|614|[Second Degree Follower](https://leetcode.com/problems/second-degree-follower/)|[Solution](../master/database/_614.sql) | | | Medium | Inner Join
629630
|613|[Shortest Distance in a Line](https://leetcode.com/problems/shortest-distance-in-a-line/)|[Solution](../master/database/_613.sql)||| Easy|
630631
|612|[Shortest Distance in a Plane](https://leetcode.com/problems/shortest-distance-in-a-plane/)|[Solution](../master/database/_612.sql)||| Medium|

‎database/_615.sql

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--615. Average Salary: Departments VS Company
2+
--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.
3+
--Table: salary
4+
--
5+
--| id | employee_id | amount | pay_date |
6+
--|----|-------------|--------|------------|
7+
--| 1 | 1 | 9000 | 2017-03-31 |
8+
--| 2 | 2 | 6000 | 2017-03-31 |
9+
--| 3 | 3 | 10000 | 2017-03-31 |
10+
--| 4 | 1 | 7000 | 2017-02-28 |
11+
--| 5 | 2 | 6000 | 2017-02-28 |
12+
--| 6 | 3 | 8000 | 2017-02-28 |
13+
--
14+
--The employee_id column refers to the employee_id in the following table employee.
15+
--
16+
--| employee_id | department_id |
17+
--|-------------|---------------|
18+
--| 1 | 1 |
19+
--| 2 | 2 |
20+
--| 3 | 2 |
21+
--
22+
--So for the sample data above, the result is:
23+
--
24+
--| pay_month | department_id | comparison |
25+
--|-----------|---------------|-------------|
26+
--| 2017-03 | 1 | higher |
27+
--| 2017-03 | 2 | lower |
28+
--| 2017-02 | 1 | same |
29+
--| 2017-02 | 2 | same |
30+
--
31+
--Explanation
32+
--
33+
--In March, the company's average salary is (9000+6000+10000)/3 = 8333.33...
34+
--
35+
--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.
36+
--
37+
--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.
38+
--
39+
--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.
40+
41+
SELECTd1.pay_month,d1.department_id,
42+
CASE WHENd1.department_avg>c1.company_avg THEN'higher'
43+
WHENd1.department_avg<c1.company_avg THEN'lower'
44+
ELSE'same'
45+
ENDAS'comparison'
46+
FROM ((SELECT LEFT(s1.pay_date,7) pay_month,e1.department_id,AVG(s1.amount) department_avg
47+
FROM salary s1
48+
JOIN employee e1ONs1.employee_id=e1.employee_id
49+
GROUP BY pay_month,e1.department_id) d1
50+
LEFT JOIN (SELECT LEFT(pay_date,7) pay_month,AVG(amount) company_avg
51+
FROM salary
52+
GROUP BY pay_month) c1ONd1.pay_month=c1.pay_month)
53+
ORDER BY pay_monthDESC, department_id;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp