SQL 中赋值排序问题

569.Median Employee Salary
首先赋值分组排序

select b.id, b.company, b.salary from 
## 计算中位数
(select count(salary)/2 as rec , company from employee group by company) a 
join
##分组赋值排序
(select id, company, salary, 
@rank:=if(@com=company, @rank+1,1) rank ,
@com:=company from employee, (select @rank:=0,@com:=null) temp 
order by company, salary) b  on a.company=b.company 
##选取排序在中位数中的值
where b.rank in (a.rec, a.rec+1, a.rec+0.5);

你可能感兴趣的:(SQL 中赋值排序问题)