LeetCode:Database 44.项目员工 III

Posted Xiao Miao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode:Database 44.项目员工 III相关的知识,希望对你有一定的参考价值。

要求:写 一个 SQL 查询语句,报告在每一个项目中经验最丰富的雇员是谁。如果出现经验年数相同的情况,请报告所有具有最大经验年数的员工。

项目表 Project的结构:

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| project_id  | int     |
| employee_id | int     |
+-------------+---------+
(project_id, employee_id) 是这个表的主键
employee_id 是员工表 Employee 的外键

员工表 Employee的结构:

+------------------+---------+
| Column Name      | Type    |
+------------------+---------+
| employee_id      | int     |
| name             | varchar |
| experience_years | int     |
+------------------+---------+
employee_id 是这个表的主键

Project 表:

+-------------+-------------+
| project_id  | employee_id |
+-------------+-------------+
| 1           | 1           |
| 1           | 2           |
| 1           | 3           |
| 2           | 1           |
| 2           | 4           |
+-------------+-------------+

Employee 表:

+-------------+--------+------------------+
| employee_id | name   | experience_years |
+-------------+--------+------------------+
| 1           | Khaled | 3                |
| 2           | Ali    | 2                |
| 3           | John   | 3                |
| 4           | Doe    | 2                |
+-------------+--------+------------------+

Result Table:

+-------------+---------------+
| project_id  | employee_id   |
+-------------+---------------+
| 1           | 1             |
| 1           | 3             |
| 2           | 1             |
+-------------+---------------+
employee_id 为 1 和 3 的员工在 project_id 为 1 的项目中拥有最丰富的经验。在 project_id 为 2 的项目中,employee_id 为 1 的员工拥有最丰富的经验。

SQL语句:

select s1 as project_id ,s2 as employee_id
from(
select s1,s2,
dense_rank() over(partition by s1
order by s3 desc ) as r
from
(select a.project_id as s1,a.employee_id as s2,
b.experience_years as s3
from project a
join employee b
on a.employee_id=b.employee_id)c
)d
where r=1;

以上是关于LeetCode:Database 44.项目员工 III的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode:Database 43.项目员工II

LeetCode:Database 17.员工奖金

LeetCode:Database 13.员工薪水中位数

LeetCode:Database 90.使用唯一标识码替换员工ID

LeetCode:Database 19.查询员工的累计薪水

LeetCode:Database 102.计算税后工资