题解 | 统计每个用户的平均刷题数(两种解法)

统计每个用户的平均刷题数

https://www.nowcoder.com/practice/f4714f7529404679b7f8909c96299ac4

# 两种解法

# 解法一(where条件,注意要在group by之前写where,group by之后写having)
select
    a.university,
    c.difficult_level,
    round(count(b.question_id) / count(distinct a.device_id), 4) as avg_answer_cnt
from
    user_profile as a
    inner join question_practice_detail as b on a.device_id = b.device_id
    inner join question_detail as c on b.question_id = c.question_id
where
    a.university = '山东大学'
group by
    a.university,
    c.difficult_level;

# 解法二(直接在inner join的on条件里写过滤条件也行)
select
    a.university,
    c.difficult_level,
    round(count(b.question_id) / count(distinct a.device_id), 4) as avg_answer_cnt
from
    user_profile as a
    inner join question_practice_detail as b on a.device_id = b.device_id and a.university = '山东大学'
    inner join question_detail as c on b.question_id = c.question_id
group by
    a.university,
    c.difficult_level;

全部评论

相关推荐

10-10 01:10
已编辑
深圳大学 测试开发
面了100年面试不知...:六月到九月,四个项目一个实习,是魔丸吗
投了多少份简历才上岸
点赞 评论 收藏
分享
深夜焦虑难以入眠:直通终面也很稳了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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