王廷瑋|數位醫療|智慧醫療: 185. Department Top Three Salaries WFU

2024年7月11日 星期四

185. Department Top Three Salaries

185. Department Top Three Salaries


員工表

+--------------+---------+ 
| 列名 | 類型 | 
+--------------+---------+ 
| id | int | 
| name | varchar | 
| salary | int | 
| departmentId | int | 
+--------------+---------+

id 是這個表的主鍵(具有唯一值的列)。 departmentId 是一個外鍵(引用列),引用自 Department 表的 ID。 這個表的每一行表示一個員工的 ID、姓名、薪水以及他們所在部門的 ID。

部門表

+-------------+---------+ 
| 列名 | 類型 | 
+-------------+---------+ 
| id | int | 
| name | varchar | 
+-------------+---------+

id 是這個表的主鍵(具有唯一值的列)。 這個表的每一行表示一個部門的 ID 和名稱。
問題說明

公司的高管希望看到每個部門收入最高的員工。部門中的高收入者是指在該部門中薪水排名前三(唯一薪水)的員工。

撰寫一個解決方案來查找每個部門中的高收入者。

可以按任意順序返回結果表。

範例:

輸入: 

員工表: 
+----+-------+--------+--------------+ 
| id | name | salary | departmentId | 
+----+-------+--------+--------------+
 | 1 | Joe | 85000 | 1 | 
| 2 | Henry | 80000 | 2 | 
| 3 | Sam | 60000 | 2 | 
| 4 | Max | 90000 | 1 | 
| 5 | Janet | 69000 | 1 | 
| 6 | Randy | 85000 | 1 | 
| 7 | Will | 70000 | 1 | 
+----+-------+--------+--------------+

部門表: 
+----+-------+
 | id | name | 
+----+-------+ 
| 1 | IT |
 | 2 | Sales | 
+----+-------+

輸出:

 +------------+----------+--------+
 | Department | Employee | Salary | 
+------------+----------+--------+ 
| IT | Max | 90000 | 
| IT | Joe | 85000 | 
| IT | Randy | 85000 | 
| IT | Will | 70000 | 
| Sales | Henry | 80000 | 
| Sales | Sam | 60000 | 
+------------+----------+--------+

解釋: 

在 IT 部門:Max 獲得了最高的唯一薪水
Randy 和 Joe 都獲得了第二高的唯一薪水
Will 獲得了第三高的唯一薪水

在 Sales 部門:
Henry 獲得了最高薪水
Sam 獲得了第二高薪水
由於只有兩名員工,因此沒有第三高薪水


MySQL


WITH RankedSalaries AS (
SELECT
e.departmentId,
e.name,
e.salary,
DENSE_RANK() OVER (PARTITION BY e.departmentId ORDER BY e.salary DESC) as salary_rank
FROM
Employee e
)
SELECT
d.name AS Department,
rs.name AS Employee,
rs.salary AS Salary
FROM
RankedSalaries rs
JOIN
Department d ON rs.departmentId = d.id
WHERE
rs.salary_rank <= 3
ORDER BY
d.name, rs.salary DESC, rs.name;

804ms


Pandas


import pandas as pd

def top_three_salaries(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame:
# Merge employee and department dataframes
df = employee.merge(department, left_on='departmentId', right_on='id', suffixes=('_employee', '_department'))
# Rank salaries within each department
df['salary_rank'] = df.groupby('departmentId')['salary'].rank(method='dense', ascending=False)
# Filter for top 3 salaries
result = df[df['salary_rank'] <= 3]
# Select and rename columns for output
result = result[['name_department', 'name_employee', 'salary']]
result.columns = ['Department', 'Employee', 'Salary']
# Sort the result
result = result.sort_values(['Department', 'Salary'], ascending=[True, False])
return result

72.38MB, 456ms


PostgreSQL


-- Write your PostgreSQL query statement below
WITH RankedSalaries AS (
SELECT
e.departmentId,
e.name AS employee_name,
e.salary,
DENSE_RANK() OVER (PARTITION BY e.departmentId ORDER BY e.salary DESC) AS salary_rank
FROM
Employee e
)
SELECT
d.name AS Department,
rs.employee_name AS Employee,
rs.salary AS Salary
FROM
RankedSalaries rs
JOIN
Department d ON rs.departmentId = d.id
WHERE
rs.salary_rank <= 3
ORDER BY
d.name, rs.salary DESC, rs.employee_name;

0.00MB, 243ms