思路:
①两表连接,用case when将分数分档,同时用count()over()窗口函数计算每个level对应的总答题数,形成表tb1;
②通过对tb1子查询,选出分档、计算比例ratio。

select level, score_grade, 
round(count(score_grade)/total_s,3) as ratio
from
(
select level, 
case when score<60 then '差'
when score>=60 and score<75 then '中'
when score>=75 and score<90 then '良'
else '优' end as score_grade,
count(level)over(partition by level) as total_s
from exam_record left join user_info using(uid)
where score is not null
) as tb1
group by level, score_grade
order by level desc, ratio desc