返回介绍

solution / 0500-0599 / 0569.Median Employee Salary / README_EN

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

569. Median Employee Salary

中文文档

Description

Table: Employee

+--------------+---------+
| Column Name  | Type  |
+--------------+---------+
| id       | int   |
| company    | varchar |
| salary     | int   |
+--------------+---------+
id is the primary key (column with unique values) for this table.
Each row of this table indicates the company and the salary of one employee.

 

Write a solution to find the rows that contain the median salary of each company. While calculating the median, when you sort the salaries of the company, break the ties by id.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
Employee table:
+----+---------+--------+
| id | company | salary |
+----+---------+--------+
| 1  | A     | 2341   |
| 2  | A     | 341  |
| 3  | A     | 15   |
| 4  | A     | 15314  |
| 5  | A     | 451  |
| 6  | A     | 513  |
| 7  | B     | 15   |
| 8  | B     | 13   |
| 9  | B     | 1154   |
| 10 | B     | 1345   |
| 11 | B     | 1221   |
| 12 | B     | 234  |
| 13 | C     | 2345   |
| 14 | C     | 2645   |
| 15 | C     | 2645   |
| 16 | C     | 2652   |
| 17 | C     | 65   |
+----+---------+--------+
Output: 
+----+---------+--------+
| id | company | salary |
+----+---------+--------+
| 5  | A     | 451  |
| 6  | A     | 513  |
| 12 | B     | 234  |
| 9  | B     | 1154   |
| 14 | C     | 2645   |
+----+---------+--------+
Explanation: 
For company A, the rows sorted are as follows:
+----+---------+--------+
| id | company | salary |
+----+---------+--------+
| 3  | A     | 15   |
| 2  | A     | 341  |
| 5  | A     | 451  | <-- median
| 6  | A     | 513  | <-- median
| 1  | A     | 2341   |
| 4  | A     | 15314  |
+----+---------+--------+
For company B, the rows sorted are as follows:
+----+---------+--------+
| id | company | salary |
+----+---------+--------+
| 8  | B     | 13   |
| 7  | B     | 15   |
| 12 | B     | 234  | <-- median
| 11 | B     | 1221   | <-- median
| 9  | B     | 1154   |
| 10 | B     | 1345   |
+----+---------+--------+
For company C, the rows sorted are as follows:
+----+---------+--------+
| id | company | salary |
+----+---------+--------+
| 17 | C     | 65   |
| 13 | C     | 2345   |
| 14 | C     | 2645   | <-- median
| 15 | C     | 2645   | 
| 16 | C     | 2652   |
+----+---------+--------+

 

Follow up: Could you solve it without using any built-in or window functions?

Solutions

Solution 1

# Write your MySQL query statement below
WITH
  t AS (
    SELECT
      *,
      ROW_NUMBER() OVER (
        PARTITION BY company
        ORDER BY salary ASC
      ) AS rk,
      COUNT(id) OVER (PARTITION BY company) AS n
    FROM Employee
  )
SELECT
  id,
  company,
  salary
FROM t
WHERE rk >= n / 2 AND rk <= n / 2 + 1;

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

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

发布评论

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