
请你写一个sql语句查询各个岗位分数的中位数位置上的所有grade信息,并且按id升序排序,结果如下:

第1行表示C++岗位的中位数位置上的为用户id为2,分数为10000,在C++岗位里面排名是第2
第2,3行表示Java岗位的中位数位置上的为用户id为4,5,分数为12000,13000,在Java岗位里面排名是第2,1
第4行表示B语言岗位的中位数位置上的为用户id为7,分数为11000,在前端岗位里面排名是第2
(注意: sqlite 1/2得到的不是0.5,得到的是0,只有1*1.0/2才会得到0.5,sqlite四舍五入的函数为round,sqlite不支持floor函数,支持cast(x as integer) 函数,不支持if函数,支持case when …then …else …end函数,sqlite不支持自定义变量)
MySql解法
select t1.id, t1.job, t1.score, t1.rn as 't_rank'
from (select id, job, score, rank() over(partition by job order by score desc) as rn,rank() over(partition by job order by score) as dn,count(1) over(partition by job) as cnt from grade) as t1
where (cnt%2=1 and rn=dn) or (cnt%2=0 and abs(cast(rn as signed) - cast(dn as signed))=1)
order by id