题目:请你写一个sql语句查询各个岗位分数升序排列之后的中位数位置的范围,并且按job升序排序,

思路:

函数:floor(),功能:向下取整数 得到为偶数时中间数开始的数(方法二)

case when 1 then 2 else 3 end ,解释:当1时,就得到2,否则得到3(方法一)

答案:

方法一:

select g.job, case when count(score) % 2 = 0 then ceiling(count()/2) else ceiling(count()/2) end as start, case when count(score) % 2 = 0 then ceiling(count()/2 + 1) else ceiling(count()/2) end as end from grade as g group by g.job order by g.job

方法二:

select g.job,

floor((count(*) + 1)/2) as start,

floor((count(*) + 2)/2) as end

from grade as g

group by g.job

order by g.job