题解 | #筛选限定昵称成就值活跃日期的用户#

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

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

具体思路: 先用窗口函数将不同级别的总人数统计出来, 优良中差这个直接case when 即可判断 这里计算比例我使用的是count()/avg(total),不是很明白为什么可以count() / total

select level,
    score_grade,
    # 这里还能 count(*) / total, 不是很理解这里, 因为已经根据level, score_grade分组了
    # 为什么还能再分组后直接使用total列?有没有大佬能解释一下. 还是说这里用到的total就是分组后第一个遇到的total值?
    round(count(*) / avg(total), 3) ratio
from (
    select bb.level,
        case when score >= 90 then '优'
            when score >= 75 then '良'
            when score >= 60 then '中'
            else '差' end score_grade,
        count(aa.uid) over(partition by level) total
    from exam_record aa
    join user_info bb on aa.uid=bb.uid
    where score is not null
) t1
group by level, score_grade
order by level desc, ratio desc
全部评论

相关推荐

2 2 评论
分享
牛客网
牛客企业服务