Oracle 11g:编写一个查询,列出每个部门的最高收入者

Oracle 11g: Write a query that lists the highest earners for each department

这个问题我已经花了几个小时解决,并尝试了各种不同的方法。 必须使用子查询

"Write a query that lists the highest earners for each department. Include the last_name, department_id, and the salary for each employee."

我做了很多子查询方法,但没有任何效果。我要么得到一个错误,要么 "No rows return"。我假设是因为 department_id 之一为空,但即使使用 NVL(department_id),我仍然遇到问题。我尝试拆分 table,但没有成功。教科书没用,我的导师有点没用,请...任何帮助。

如果有帮助的话,这是值的快照。 https://www.dropbox.com/s/bxtntlzqixdizzp/helpme.png?dl=0

您可以对每个部门内的值进行排名 - 然后在外部查询中仅拉出第一名。

select a.last_name
,a.department_id
,a.salary
from (
  select last_name
  ,department_id
  ,salary
  ,rank() over (partition by department_id order by salary desc) as rnk
  from tablename
) a
where rnk=1

分区将共享同一部门的所有员工分组在一起,并且无论空值如何都应该工作。

将它们分组后 - 排序依据告诉该组按薪水降序排序,并给出排名。您可以 运行 仅通过内部查询了解它的作用。