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

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

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

题意明确:

仅查看山东大学的用户在不同难度下的每个用户的平均答题题目数


问题分解:

  • 限定条件:山东大学的用户 up.university="山东大学"
  • 不同难度:按难度分组group by difficult_level
  • 平均答题数:总答题数除以总人数count(qpd.question_id) / count(distinct qpd.device_id) 来自上面信息三个表,需要联表,up与qpd用device_id连接并限定大学,qd与qpd用question_id连接。

细节问题:

  • 表头重命名:as
  • 平均值精度未明确要求,忽略

完整代码:

select 
    "山东大学" as university,
    difficult_level,
    count(qpd.question_id) / count(distinct qpd.device_id) as avg_answer_cnt
from question_practice_detail as qpd

inner join user_profile as up
on up.device_id=qpd.device_id and up.university="山东大学"

inner join question_detail as qd
on qd.question_id=qpd.question_id

group by difficult_level
全部评论
牛客的用户表有没有显示的字段,所以左连接不行
1 回复 分享
发布于 2022-04-21 22:06
使用group by的时候,select子句中的列名必须为「分组列」或「聚合函数」 group by里面只有 difficult_level的话,select语句里面可以有university吗?
7 回复 分享
发布于 2021-10-28 15:30
为什么要使用内连接呀?
4 回复 分享
发布于 2022-03-09 22:00
请教一下 这一步的含义 "山东大学" as university,
2 回复 分享
发布于 2023-03-09 18:04 广东
如果是左外连接left join ,on后边的关于左表的限定语句则不生效
2 回复 分享
发布于 2023-01-20 12:22 山东
左连接会出现null,然后在统计的时候,结果也会出现null行
1 回复 分享
发布于 2022-08-09 14:35
left join不行,要用inner join
1 回复 分享
发布于 2022-03-15 11:34
你好,我想问下,我的表的顺序跟你写的一样,也是qpd开始,不过我用的是左连接,怎么报错了。。改回内连接就好了
1 回复 分享
发布于 2022-03-02 15:43
为啥不能用left join呢
点赞 回复 分享
发布于 2023-09-01 14:50 上海
大神们帮忙看看这样合理不? select a.university, c.difficult_level, round(count(b.question_id) / count(distinct b.device_id),4 ) as avg_answer_cnt from question_practice_detail as b left join user_profile a on b.device_id = a.device_id left join question_detail as c on b.question_id = c.question_id where a.university='山东大学' group by c.difficult_level
点赞 回复 分享
发布于 2023-03-09 18:17 广东
select university, difficult_level, count(qpd.question_id) / count(distinct qpd.device_id) as avg_answer_cnt from question_practice_detail as qpd inner join user_profile as up on up.device_id=qpd.device_id and up.university="山东大学" inner join question_detail as qd on qd.question_id=qpd.question_id group by difficult_level;
点赞 回复 分享
发布于 2022-11-18 16:18 浙江
SQL_ERROR_INFO: "Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'up.university' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by" 这个问题是什么呢 求大神解决
点赞 回复 分享
发布于 2022-02-08 17:17
连接语法不是from table1 join table2 on table1=table2 吗?为啥这里反了?
点赞 回复 分享
发布于 2022-01-11 16:43

相关推荐

点赞 评论 收藏
分享
评论
168
23
分享

创作者周榜

更多
牛客网
牛客企业服务