题解 | #统计每个学校的答过题的用户的平均答题数#

统计每个学校的答过题的用户的平均答题数

https://www.nowcoder.com/practice/88aa923a9a674253b861a8fa56bac8e5

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

这个写也很好,只要能明白对于每个学校,用答题的数量/答题的人数即可。

这道题的难度,就有点上升了开始。

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务