Department Top Three Salaries

1430 ワード

表内の自己接続、rankの上位3の記録を得るhttps://leetcode.com/problems/department-top-three-salaries/
SELECT D.Name AS Department,A.Employee,A.Salary FROM (SELECT C.DepartmentId,C.Name AS Employee,C.Salary FROM (SELECT E1.id,E1.Name,E1.Salary,E1.DepartmentId,COUNT(1) AS RANK FROM Employee E1 INNER JOIN (SELECT DISTINCT Salary,DepartmentId FROM Employee) E2 ON E1.DepartmentId=E2.DepartmentId AND E1.Salary<=E2.Salary GROUP BY E1.id,E1.Name,E1.Salary,E1.DepartmentId) C WHERE C.RANK<4) A INNER JOIN Department D ON D.Id=A.DepartmentId