Highest Salary in each department

前端 未结 30 1450
没有蜡笔的小新
没有蜡笔的小新 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 23:13

    with ctesal as (
     select DepartmentId , Name , Salary, ROW_Number() OVER (partition by DepartmentId 
     order by Salary desc) as RowNum
     from dbo.Employee
     )
     select DepartmentId , Name , Salary , RowNum from ctesal where RowNum  =2;
    

    This is applicable to SQL server. ROW_Number is a inbuilt function in SQL server .It gives count starting from 1 based on partition by and order by clause. At the end, We can write where condition based on our requirements.

提交回复
热议问题