Select EMP with max SAL from each DEPT

前端 未结 4 1279
予麋鹿
予麋鹿 2021-01-25 22:38

I´m having a bad time with a SQL query. I´m using oracle default tables:

\'EMP\' TABLE

http://imageshack.us/photo/my-images/850/sinttuloxps.png/

AND

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-25 23:21

    You can also use the analytical RANK() function:

    SELECT * FROM (
      SELECT
        Dept.DeptNo,
        Dept.DName,
        Emp.EName,
        Emp.Sal,
        RANK() OVER (PARTITION BY Dept.DeptNo ORDER BY Emp.Sal DESC) AS DeptSalRank
      FROM Emp
      INNER JOIN Dept ON Emp.DeptNo = Dept.DeptNo
    )
    WHERE DeptSalRank = 1
    

提交回复
热议问题