184. 部门工资最高的员工 select d.Name as Department, e.Name as Employee, e.Salary from Employee e,Department d where e.DepartmentId=d.id and (e.Salary,e.DepartmentId) in (select max(Salary),DepartmentId from Employee group by DepartmentId);
/* Write your T-SQL query statement below */ -- 每个部门最高 SELECT S.NAME, S.EMPLOYEE, S.SALARY FROM (SELECT D.NAME, T.NAME EMPLOYEE, T.SALARY, ROW_NUMBER() OVER(PARTITION BY T.DEPARTMENTID ORDER BY T.SALARY DESC) RN FROM EMPLOYEE T LEFT JOIN DEPARTMENT D ON T.DEPARTMENTID = D.ID) S WHERE S.RN = 1 -- 每个部门前2高 SELECT S.NAME, S.EMPLOYEE, S.SALARY FROM (SELECT D.NAME, T.NAME EMPLOYEE, T.SALARY, ROW_NUMBER() OVER(PARTITION BY T.DEPARTMENTID ORDER BY T.SALARY DESC) RN FROM EMPLOYEE T LEFT JOIN DEPARTMENT D ON T.DEPARTMENTID = D.ID) S WHERE S.RN <= 2 -- 每个部门第一第三高 SELECT S.NAME, S.EMPLOYEE, S.SALARY FROM (SELECT D.NAME, T.NAME EMPLOYEE, T.SALARY, ROW_NUMBER() OVER(PARTITION BY T.DEPARTMENTID ORDER BY T.SALARY DESC) RN FROM EMPLOYEE T LEFT JOIN DEPARTMENT D ON T.DEPARTMENTID = D.ID) S WHERE S.RN = 1 OR S.RN = 3