我在Leetcode(https://leetcode.com/problems/department-highest-salary/description/)中处理部门最高工资问题。我尝试以下列方式使用rank()函数,但它总是给我错误。我想知道出了什么问题?
Select d.Name as 'Department', e.Name as 'Employee', e.Salary
from (select Name, DepartmentId, Salary,
rank() over (PARTITION BY DepartmentId order by Salary desc) as srank
from Employee
where srank = 1) e
join Department d
on d.Id = e.DepartmentId
我知道以下代码效果很好。
select d.name as 'Department', e.name as 'Employee', Salary
from Employee e
join Department d
on d.Id = e.DepartmentId
where (e.DepartmentId , Salary) in (select DepartmentId, max(Salary)
from Employee
group by DepartmentId)