/*题意:试卷得分按分界点[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