950ms Accepted My Answer


  • 0
    Z
    select
    d.Name as Department,
    e.Name as Employee,
    e.Salary as Salary
    from 
    (
    SELECT 
    Id, Name, Salary, DepartmentId,
    CASE
    when @pre_d = DepartmentId and @pre_s <> Salary then  @rank := @rank + 1
    when @pre_d = DepartmentId and @pre_s = Salary and @pre_s = -1  then  @rank := @rank + 1
    when @pre_d = DepartmentId and @pre_s = Salary and @pre_s <> -1 then  @rank
    when @pre_d <> DepartmentId then @rank := 1
    end as Rank,
    (@pre_d:= DepartmentId) as d,
    (@pre_s:= Salary) as s
    FROM Employee , 
    (
    SELECT 
    @rank:=0, 
    @pre_d:= -1, 
    @pre_s:= -1 
    )as r
    ORDER by DepartmentId asc, Salary desc
    ) as e
    left join Department as d
    on e.DepartmentId = d.Id
    where e.Rank <= 3
    and d.Name is not null

Log in to reply
 

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