简体   繁体   English

SQL:在列上返回具有最大值的多行

[英]SQL: return multiple rows with maximal value on a column

You can find the problem from here .你可以从这里找到问题。

When there are multiple people with the highest salary in each department, the following SQL command output the expected results - all employees with highest salary are output:当每个部门有多个薪水最高的人时,下面的SQL命令输出预期的结果——输出所有薪水最高的员工:

select d.Name as Department, e.Name as Employee, e.Salary as Salary
from 
(select Name, Salary, DepartmentId
from Employee
having (Salary, DepartmentId) in (
    select max(Salary), DepartmentId
    from Employee
    group by DepartmentId)
)e, Department d
where e.DepartmentId = d.Id

However, the following command only output one employee even if there are multiple employees with the same highest salary:但是,即使有多个工资最高的员工,以下命令也只输出一名员工:

select d.Name as Department, e.Name as Employee, e.Salary as Salary
from 
(select Name, Salary, DepartmentId
from Employee
group by DepartmentId
having Salary = max(Salary)
)e, Department d
where e.DepartmentId = d.Id

Can anyone explain to me why the latter one does not work?谁能向我解释为什么后者不起作用? Thanks!谢谢!

The reason the second query doesn't work is because it's getting the max salary regardless of the department.第二个查询不起作用的原因是因为无论部门如何,它都会获得最高工资。

The way I would have done this, despite the valid first query (which is not using the standard JOIN syntax like it should), would be to first get the highest salary for each department like this:尽管第一个查询是有效的(它没有像它应该的那样使用标准的 JOIN 语法),我会这样做的方式是首先获得每个部门的最高薪水,如下所示:

SELECT departmentID, MAX(salary)
FROM employee
GROUP BY departmentID;

Once you have that, you can use a self join to only select the rows with that salary and department.一旦有了它,您就可以使用自联接来仅选择具有该薪水和部门的行。 This will include multiple people if they have the same salary:如果他们的薪水相同,这将包括多个人:

SELECT e.*
FROM employee e
JOIN(
   SELECT departmentID, MAX(salary) AS maxSalary
   FROM employee
   GROUP BY departmentID) tmp ON tmp.departmentID = e.departmentID AND tmp.maxSalary = e.salary

For further explanation, consider just the inner subquery you have:为了进一步解释,请考虑您拥有的内部子查询:

SELECT name, department, salary
FROM employee
GROUP BY department
HAVING salary = MAX(salary);

The first problem with this is that you are selecting rows that are not consistent within your group.第一个问题是您选择的行在您的组内不一致。 For each department id, you have multiple name values and multiple salary values, which will be chosen arbitrarily when you do the grouping.对于每个部门id,你有多个name值和多个salary值,在分组的时候会任意选择。 In my example, and the first, you can see that the grouping explicitly pulls the (departmentID, salary) pair for the max salary in each department, which the second one does not.在我的示例和第一个示例中,您可以看到分组明确地为每个部门的最大工资提取 (departmentID,salary) 对,而第二个没有。

Here is an SQL Fiddle example to visually see the differences between your second query and mine.这是一个SQL Fiddle示例,可以直观地查看您的第二个查询和我的查询之间的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM