My solution........


  • 0
    H
    SELECT dep.name AS Department, emp.Name AS Employee, emp.Salary FROM (
    	SELECT 
    		DepartmentId AS did,
    		Name,
    		CASE
    			WHEN 
    				@prevdep<>departmentId AND (@topsal:=1) OR
    				Salary=@prevsal OR
    				@topsal<3 AND (@topsal:=@topsal+1)
    			THEN Salary
    			WHEN @prevdep=DepartmentId AND @topsal>3 THEN NULL
    		END AS Salary,
    		@prevsal:=Salary,
    		@prevdep:=DepartmentId
    	FROM (
    			SELECT * FROM Employee, (SELECT @topsal:=0) AS ts, (SELECT @prevdep:=0) AS pd, (SELECT @prevsal:=NULL) AS ps ORDER BY DepartmentId ASC, Salary DESC
    	) AS a
    ) AS emp
    RIGHT JOIN Department dep
    ON emp.did=dep.id
    WHERE Salary IS NOT NULL

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.