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

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

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

select
    level,
    score_grade,
    # 统计得分占比
    round(count(uid) / total, 3) as ratio
from
    (
        select
            u_i.uid as uid,
            exam_id,
            score,
            level,
            # 试卷得分分层
            case
                when score >= 90 then "优"
                when score >= 75 then '良'
                when score >= 60 then '中'
                else '差'
            end as score_grade,
        # 题目要求是统计不同用户等级的人,所以要按照level分组统计次数
            count(*) over (
                partition by
                    level
            ) as total
        from
            user_info u_i
            join exam_record e_r on u_i.uid = e_r.uid
        # 未完成试卷的用户无需输出
        where
            score is not null
    ) as user_grade_table
group by
    level,
    score_grade
order by
    level desc,
    ratio desc

全部评论

相关推荐

迷茫的大四🐶:看来已经准备换人了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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