题解 | #各用户等级的不同得分表现占比#

各用户等级的不同得分表现占比

http://www.nowcoder.com/practice/ebff819fd38c46db8a42dfe43ca7b33a

/*题意:试卷得分按分界点[90,75,60]分为优良中差四个得分等级,未完成过试卷的用户无需输出
题解: 1.首先,将存在试卷考试记录的所有得分和用户进行等级划分,后续需要计算得分等级占比,所以需要exam_id分散开
2. 各个等级用户的得分登记占比,相当于为统计出各个等级用户的优良中差次数占比总数
*/
select b.level
,b.score_grade
,round((b.num/sum(b.num)over(partition by b.level)),3) as rate
from (
    select a.level,a.score_grade
    ,count(a.score_grade) as num
    from
    (
    select e.uid,e.exam_id,u.level,e.score,
    case when e.score <60 then '差'
        when e.score <75 and e.score>= 60 then '中'
        when e.score < 90 and e.score>=75 then '良'
        when e.score >= 90 then "优"
        end as score_grade
    from exam_record e 
    left join user_info u
    on e.uid = u.uid 
    where e.score is not null
        )a
group by a.level, a.score_grade
)b
group by b.level,b.score_grade
order by b.level desc,rate desc 


数据库刷题题解 文章被收录于专栏

数据分析数据库题目练习题解

全部评论

相关推荐

04-06 16:59
已编辑
河南工业大学 Java
牛牛牛的牛子:最好扔了,实在没有选择的选择
点赞 评论 收藏
分享
刘湘_passion:出国旅游?那就小心你的腰子咯
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务