1077. Project Employees III 難度:中等

1、題目描述

Write an SQL query that reports the most experienced employees in each project. In case of a tie, report all employees with the maximum number of experience years.
The query result format is in the following example:
Project table:

project_id employee_id
1 1
1 2
1 3
2 1
2 4

Employee table:

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

Both employees with id 1 and 3 have the most experience among the employees of the first project. For the second project, the employee with id 1 has the most experience.

來源:力扣(LeetCode)

2、解題思路

主要是出現多個最多經驗的,重複數據處理
1# 首選2表聯查,找出每個project_id最大經驗值

select project_id,e.employee_id,max(experience_years) as `years`
from Project p left join Employee e 
on e.employee_id=p.employee_id
group by project_id

2# 然後,Project、Employee還有表,3表聯查,條件是experience_years等於上述最大經驗值

3、提交記錄

select p0.project_id,p0.employee_id

from Project p0 left join Employee e0
on p0.employee_id=e0.employee_id 

left join

(select project_id,e.employee_id,max(experience_years) as `years`
from Project p left join Employee e 
on e.employee_id=p.employee_id
group by project_id) max1

on p0.project_id=max1.project_id
where e0.experience_years=max1.`years`

1190ms

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章