题解 | #考试分数(四)#

考试分数(四)

http://www.nowcoder.com/practice/502fb6e2b1ad4e56aa2e0dd90c6edf3c

# 修改了一下大神的代码
SELECT job,
        floor((count(*)+1)/2) start ,
        ceiling((count(*)+1)/2) end 
from grade
group by job
order by job;

#使用窗口函数
SELECT g.job,
        if(mod(count(rak),2)=0,round(count(rak)/2),round(count(rak)/2)) start ,
        if(mod(count(rak),2)=0,round(count(rak)/2)+1,round(count(rak)/2)) end 
FROM
(
SELECT job,
    ROW_NUMBER() over (partition by job) rak
FROM grade ) g 
group by g.job
;

#使用关联子查询
select job,
        ( select if(mod(count(*),2)=0,
                    round(count(*)/2),
                    round(count(*)/2)) start                 
                    from grade g2 where g1.job=g2.job
        ) start,
         ( select if(mod(count(*),2)=0,
                    round(count(*)/2)+1,
                    round(count(*)/2)) end                
                    from grade g2 where g1.job=g2.job
        ) end
from grade g1 
group by job
order by job;
全部评论

相关推荐

迟缓的斜杠青年巴比Q...:简历被投过的公司卖出去了,我前两天遇到过更离谱的,打电话来问我有没有意向报班学Java学习,服了,还拿我学校一个学长在他们那报班学了之后干了华为OD当招牌
点赞 评论 收藏
分享
07-02 13:50
闽江学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务