with
t1 as(
    select
        level,
        (case
            when score>=90 then '优'
            when score>=75 then '良'
            when score>=60 then '中'
            else '差'
        end
        ) as score_grade,
        count(uid) as cnt
    from
        user_info inner join exam_record using(uid)
    where score is not null
    group by
        level,score_grade
),
t2 as(
    select
        level,
        score_grade,
        round((cnt/sum(cnt)over(partition by level)),3) as ratio
    from
        t1
)

select * from t2 order by level desc,ratio desc