返回介绍

solution / 0600-0699 / 0615.Average Salary Departments VS Company / README_EN

发布于 2024-06-17 01:03:36 字数 4780 浏览 0 评论 0 收藏 0

615. Average Salary Departments VS Company

中文文档

Description

Table: Salary

+-------------+------+
| Column Name | Type |
+-------------+------+
| id      | int  |
| employee_id | int  |
| amount    | int  |
| pay_date  | date |
+-------------+------+
In SQL, 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 (reference column) from the Employee table.

 

Table: Employee

+---------------+------+
| Column Name   | Type |
+---------------+------+
| employee_id   | int  |
| department_id | int  |
+---------------+------+
In SQL, employee_id is the primary key column for this table.
Each row of this table indicates the department of an employee.

 

Find 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 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.

Solutions

Solution 1

# Write your MySQL query statement below
WITH
  t AS (
    SELECT
      DATE_FORMAT(pay_date, '%Y-%m') AS pay_month,
      department_id,
      AVG(amount) OVER (PARTITION BY pay_date) AS company_avg_amount,
      AVG(amount) OVER (PARTITION BY pay_date, department_id) AS department_avg_amount
    FROM
      Salary AS s
      JOIN Employee AS e ON s.employee_id = e.employee_id
  )
SELECT DISTINCT
  pay_month,
  department_id,
  CASE
    WHEN company_avg_amount = department_avg_amount THEN 'same'
    WHEN company_avg_amount < department_avg_amount THEN 'higher'
    ELSE 'lower'
  END AS comparison
FROM t;

Solution 2

# Write your MySQL query statement below
WITH
  S AS (
    SELECT *
    FROM
      Salary
      JOIN Employee USING (employee_id)
  ),
  T AS (
    SELECT
      DATE_FORMAT(pay_date, '%Y-%m') AS pay_month,
      department_id,
      AVG(amount) OVER (PARTITION BY pay_date, department_id) AS department_avg,
      AVG(amount) OVER (PARTITION BY pay_date) AS company_avg
    FROM S
  )
SELECT
  pay_month,
  department_id,
  CASE
    WHEN AVG(department_avg) > AVG(company_avg) THEN 'higher'
    WHEN AVG(department_avg) < AVG(company_avg) THEN 'lower'
    ELSE 'same'
  END AS comparison
FROM T
GROUP BY 1, 2;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文