Highest Salary in each department

前端 未结 30 1464
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 22:16

I have a table EmpDetails:

DeptID      EmpName   Salary
Engg        Sam       1000
Engg        Smith     2000
HR          Denis     1500
HR                  


        
30条回答
  •  说谎
    说谎 (楼主)
    2021-01-30 22:55

    Assuming SQL Server 2005+

    WITH cteRowNum AS (
        SELECT DeptID, EmpName, Salary,
               DENSE_RANK() OVER(PARTITION BY DeptID ORDER BY Salary DESC) AS RowNum
            FROM EmpDetails
    )
    SELECT DeptID, EmpName, Salary
        FROM cteRowNum
        WHERE RowNum = 1;
    

提交回复
热议问题