Share my solution


  • 0
    H
    select d.name as Department, f.name as Employee, f.salary
    from
    (select id, name, salary, departmentid,
    case departmentid when @did then @rank:= if(@prevSalary = salary, @rank, @rank+1)
        else @rank:=1 and @did:=departmentid 
        end as rank,
     @prevSalary:= salary
    from employee e ,(select @rank:=0, @did:=null, @prevSalary:=null) t 
    order by departmentid,  salary desc) f
    join department d
    on f.departmentid = d.id and f.rank<=3
    

    The idea is simple: for each department, I want to find the rank of each employee.


Log in to reply
 

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