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

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

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

# 先找出完成过试卷的用户uid
with t1 as (
    select a.uid, b.level, a.score
    from exam_record as a
    left join user_info as b
    on a.uid = b.uid
),
# 将各个分数所对应的等级表示出来
t2 as (
    select uid, level, 
    (case when score < 60 then '差'
    when score >= 90 then '优'
    when score >= 75 and score < 90 then '良'
    else '中' end) as score_grade
    from t1
    where score IS NOT NULL
),
# 根据每个用户的等级的不同分数性质进行分组,统计个数
t3 as (
    select level, score_grade, count(uid) as cnt
    from t2
    group by level, score_grade
),
# 根据用户等级进行分组
t4 as (
    select level, count(uid) as total_cnt
    from t2 
    group by level
)
# 最后左连接进行比率的计算
select t3.level, t3.score_grade, round(t3.cnt/t4.total_cnt, 3) as ratio
from t3
left join t4
on t3.level = t4.level
order by level desc, ratio desc;

全部评论
本蒟蒻在最后的比率计算代码又臭又长
点赞 回复 分享
发布于 2023-08-13 00:36 江苏

相关推荐

10-28 10:48
已编辑
门头沟学院 Java
孩子我想要offer:发笔试后还没笔试把我挂了,然后邮箱一直让我测评没测,后面不知道干嘛又给我捞起来下轮笔试,做完测评笔试又挂了😅
点赞 评论 收藏
分享
12-20 11:21
复旦大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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