题目来源https://leetcode.com/problems/department-top-three-salaries/
这道题和之前的Department Highest Salary差不多,只是从每个部门最高工资换成输出每个部门前三名的工资。
实在做不出来,后来查到一个很不错的想法。
选出所在部门工资超过他的不超过三人的员工,代码如下:
select D.Name, E.Name, E.Salary
from Employee E, Department D
where (select count(distinct(Salary)) from Employee
where DepartmentId = E.DepartmentId and Salary > E.Salary) in (0, 1, 2)
and
E.DepartmentId = D.Id
order by E.DepartmentId, E.Salary DESC;
最后按部门和工资降序排序
终