审题可知以下条件
1.浙江大学用户,即 user_profile.university='浙江大学';
2.不同难度题目的正确率,需要先 join question_practice_detail 表和 question_detail 表,得到答过题目的难度情况;
3.最后 join 1的结果和2的结果,题目难度分组,聚合不同难度的题目数,以及答对题目数,相除即得到正确率;
考察
1.各种 join 的用法;
2.case when 和聚合函数一起使用的情况
完整代码
-- 1.浙江大学用户 with us as ( select id, device_id, university from user_profile where university='浙江大学' ), -- 2.答题明细 que as ( select qpd.*, qd.difficult_level from question_practice_detail as qpd left join question_detail as qd on qpd.question_id=qd.question_id ) select que.difficult_level, round(sum(case when que.result='right' then 1 else 0 end)/count(que.question_id),4) as correct_rate from que inner join us on que.device_id=us.device_id group by que.difficult_level order by correct_rate asc