select t.university,round(avg(num),4) as avg_answer_cnt from -- 思路先求出每个用户答题数 (select device_id,count(*) as num from question_practice_detail group by device_id) t1 inner join user_profile t on t.device_id = t1.device_id group by t.university order by t.university
我首先想到的是,先求出每个用户的答题数,然后表链接,根据每个用户的学校,再继续往下。
由于一开始没有对学校排序,报错了几次,就是顺序不对。
后面看了一下评论区,发现大家都是以 下面的代码为主
select t.university,round(count(t1.question_id)/count(distinct(t1.device_id)),4) as avg_answer_cnt from user_profile t inner join question_practice_detail t1 on t.device_id =t1.device_id group by t.university order by t.university
这个写也很好,只要能明白对于每个学校,用答题的数量/答题的人数即可。
这道题的难度,就有点上升了开始。