下面是我的表,我试图编写一个MySQL查询,选择每个部门(dID)中薪酬最高的员工(Salary+Comm)

EmployeeID  | Name       | mgr | Job         | Salary| Com | HireDate     | dID
1001 | Ron Smith   | 1005| Writer| 90000 | 20000| 20012-04-12| 1
1002 | Ricky Lake  | 1003| Writer| 55000 | 15000| 2013-01-18| 1

这就是我目前所拥有的
SELECT dID, MAX(coalesce(Salary+Comm, Salary, Comm)) AS 'TotalPaid'
FROM Employee
Group By dID

如何才能允许显示其他列?希望我已经解释了我想要的东西,谢谢。

最佳答案

有一种方法:

select e.* from
Employee e inner join
(SELECT dID, MAX(coalesce(Salary+Comm, Salary, Comm)) as max_salary  AS 'TotalPaid'
FROM Employee
Group By dID) d
on d.dID = e .dID
and coalesce(e.Salary+e.Comm, e.Salary, e.Comm)=d.max_salary

关于mysql - 查找每个部门(dID)中收入最高的员工(Salary + Comm),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55193499/

10-16 22:09