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

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

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 江苏

相关推荐

赛博小保安:你这简历没啥大问题的,经历技能也足够了,问题应该就是出在出身了,学院本就是这样,HR忙着跟92的勾搭呢,哪有心思看我们这些双非😿😭
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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