A solution can run on my computer but can't on LeetCode


  • -1
    S

    set @num=1,@DepartmentI=0;
    select b.Name as Department,a.Name as Employee,a.Salary from Employee a inner join Department b on a.DepartmentId=b.Id where (a.Salary,a.DepartmentId) in
    (select Salary,DepartmentId from
    (select *,if(@DepartmentI=DepartmentId,@num:=@num+1,@num:=1)as row_num,@DepartmentI:=DepartmentId as s from
    (select distinct Salary,DepartmentId from Employee order by DepartmentId,Salary DESC)a)b where b.row_num<4) order by b.Id,a.Salary DESC


  • -1
    S

    select b.Name as Department,a.Name as Employee,a.Salary from Employee a inner join Department b on a.DepartmentId=b.Id where (a.Salary,a.DepartmentId) in
    (select Salary,DepartmentId from
    (select *,if(@DepartmentI=DepartmentId,@num:=@num+1,@num:=1)as row_num,@DepartmentI:=DepartmentId as s from
    (select distinct Salary,DepartmentId,@num:=1,@DepartmentI:=0 from Employee order by DepartmentId,Salary DESC)a)b where b.row_num<4) order by b.Id,a.Salary DESC,Anybody can help me optimize the code or show your answer for me?


  • -1
    S

    select b.Name as Department,a.Name as Employee,a.Salary from Employee a inner join Department b on
    a.DepartmentId=b.Id where 3>(select count(*) from(select distinct Salary,DepartmentId from Employee)c where c.Salary>a.Salary and c.DepartmentId=a.DepartmentId)
    there is an other method code,although this one is much slower than the one before,I think it is much better than before


Log in to reply
 

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