题解 | #浙大不同难度题目的正确率#

浙大不同难度题目的正确率

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

题意明确:

浙江大学的用户在不同难度题目下答题的正确率情况,按照准确率升序输出


问题分解:

  • 限定条件:浙江大学的用户;
  • 不同难度:difficult_level(question_detail表中的列),需要分组统计,因此用到group by,语法详情
  • 正确率:表面理解就是正确数÷总数,正确的是result='right'(question_practice_detail表),数目用函数count,总数是count(question_id);
  • 多张表联合查询:需要用到join,join有多种语法,因为条件限定需要是浙江大学的用户,所以需要是user_profile表的并且能统计出题目难度的记录,因此用user_profile表inner join另外两张表。
  • join语法:语法详解,图解: 图片说明

细节问题:

  • 表头重命名:根据输出示例,正确率用as语法重命名
  • 升序输出:order by xxx asc
  • 正确率的计算方式:判断result是否为right,是的话赋值为1,对于正确的数目,可以用count,也可以用sum,正确率还可以直接用avg计算。
  • join方式选择:如果前面inner join改成left join,为了防止结果中有难度为None的结果,需要在order by前加一句 having qd.difficult_level != 'None'

因此完整代码呼之欲出:

select difficult_level,
    avg(if(qpd.result='right', 1, 0)) as correct_rate
#    sum(if(qpd.result='right', 1, 0)) / count(qpd.question_id) as correct_rate
#    count(if(qpd.result='right', 1, null)) / count(qpd.question_id) as correct_rate
from user_profile as up

inner join question_practice_detail as qpd
    on up.device_id = qpd.device_id

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

where up.university = '浙江大学'
group by qd.difficult_level
order by correct_rate asc;
全部评论
《呼之欲出》
36 回复 分享
发布于 2021-11-12 22:52
提交的第三次测试会出现一个没有答题记录的浙江大学学生,会导致left join 出现一个null值记录 。 解决方法:1.像作者一样使用inner join或限制null值 2.left join切换主表,将主表从user_profile切换到question_practice_detail,则不会出现null值记录 切换主表后代码如下:select t3.difficult_level, count(if(result='right', 1,null))/count(result) as correct_rate from question_practice_detail as t2 left join user_profile as t1 on t1.device_id = t2.device_id left join question_detail as t3 on t2.question_id = t3.question_id where t1.university = '浙江大学' group by t3.difficult_level order by correct_rate;
18 回复 分享
发布于 2022-01-19 11:30
谢谢你的解答。对于一个1&0序列,取平均就是1的占比。自己做的时候,没想到可以这样写。
18 回复 分享
发布于 2021-11-14 14:10
avg(if(qpd.result='right', 1, 0)) --这段能不能解释一下用法?谢谢
9 回复 分享
发布于 2021-11-13 15:30
求正确率还有三种做法,强啊
8 回复 分享
发布于 2021-12-22 14:46
count(if(q.result='right',1,null) 为什么不能用count(if(q.result='right',1,0) ??求解答 谢谢
4 回复 分享
发布于 2022-02-23 13:54
如果用left join ,为什么会出现难度为none的结果?在创建该表时不是限制“难度”这一列为NOT NULL吗
2 回复 分享
发布于 2021-12-07 11:30
我打印了一下正确数和总数,数量好像不对啊。我在数据表里数了一下,简单的做了两题,对了一题,但打印的是做了四题,对了两题。 虽然答案是对的,但数据不对。。。
1 回复 分享
发布于 2022-09-23 16:38 江西
一开始傻傻的想正确率用啥函数,没想到avg就能更方便,当然三种方都很OK 学到了,受教!!!
1 回复 分享
发布于 2022-05-25 16:05
大佬能不能解释一下using()和on 的区别,我在运行的时候发现on qpd.device_id=up.device_id这样确定表联结的方式报错,但是如果using(device_id)就不会,搞不懂这二者有很大差别吗?
1 回复 分享
发布于 2022-02-23 22:32
越来越难了
点赞 回复 分享
发布于 08-04 22:49 湖南
select b3.difficult_level,count(case when b2.result='right'then 1 end)/count(b2.question_id)correct_rate from question_practice_detail b2 join question_detail b3 on b2.question_id=b3.question_id where device_id in (select b1.device_id from user_profile b1 where b1.university='浙江大学') group by b3.difficult_level 有没有大佬讲讲为什么自测对,保存就不对了
点赞 回复 分享
发布于 01-19 12:15 山东
写对了一道困难的题 好有成就感
点赞 回复 分享
发布于 2024-07-04 16:52 广东
select difficult_level,count(if(result='right',question_id,null))/count(question_id) as correct_rate from question_practice_detail as a left join question_detail as b using (question_id) where device_id in (select distinct device_id from user_profile where university='浙江大学') group by difficult_level;为什么这是错的
点赞 回复 分享
发布于 2024-04-22 10:11 福建
请教一下大家,为什么不能用sum(if(qpd.result='right',1,0))/answer_cnt 呢?answer_cnt不是答了多少题目吗?我每次都不知道什么时候能用avg 函数,我怕电脑不知道分母是啥T.T
点赞 回复 分享
发布于 2024-04-11 15:50 广东
请问下我这样连接有什么问题吗? select qd.difficult_level,sum(if(qpd.result='right',1,0))/count(qpd.question_id) as correct_rate from user_profile as up,question_practice_detail as qpd,question_detail as qd where up.device_id=qpd.device_id and qpd.question_id=qd.question_id and up.university='浙江大学' group by qd.difficult_level order by correct_rate
点赞 回复 分享
发布于 2024-04-01 20:20 四川
为什么 select AA3.difficult_level, sum(case when AA2.result='right' then 1 else 0 end)/count(AA2.question_id) as correct_rate from question_practice_detail AA2 left join user_profile as AA1 on AA1.device_id=AA2.device_id left join question_detail AA3 on AA3.question_id=AA2.question_id WHERE AA1.university='浙江大学' group by AA3.difficult_level order by correct_rate 和 select AA3.difficult_level, sum(case when AA2.result='right' then 1 else 0 end)/count(AA2.question_id) as correct_rate from question_practice_detail AA2 left join (select * from user_profile where university='浙江大学') as AA1 on AA1.device_id=AA2.device_id left join question_detail AA3 on AA3.question_id=AA2.question_id group by AA3.difficult_level order by correct_rate 输出的值不一样呢
点赞 回复 分享
发布于 2024-01-20 22:37 广东
膜拜!!!!
点赞 回复 分享
发布于 2023-04-12 09:43 陕西
SELECT c.difficult_level,SUM(IF(b.result='right',1,0))/COUNT(b.question_id) correct_rate FROM user_profile a,question_practice_detail b,question_detail c WHERE a.device_id=b.device_id AND a.university='浙江大学' AND b.question_id=c.question_id GROUP BY c.difficult_level ORDER BY correct_rate asc 不习惯left join,这样不香吗
点赞 回复 分享
发布于 2023-04-10 15:07 浙江
提示“程序异常退出, 请检查代码"是否有数组越界等异常"或者"是否有语法错误" SQL_ERROR_INFO: "execute command denied to user 'root'@'localhost' for routine 'count'"” 麻烦哪位好心大佬帮忙看看哪里有问题,先磕个头! select difficult_level, sum(if (qpd.result=' right ', 1, 0))/count (qpd.question_id) as correct_rate from question_practice_detail qpd left join question_detail qd on qpd.question_id=qd.question_id left join user_profile up on qpd.device_id=up.device_id where up.university='浙江大学' group by qd.difficult_level order by correct_rate asc
点赞 回复 分享
发布于 2022-10-11 14:39 广东

相关推荐

11-25 22:06
已编辑
华为 2012基座大模型(预研) 15A 硕士985
点赞 评论 收藏
分享
安静的鲸鱼offer...:神仙级别hr,可遇不可求,甚至他可能也是突然有感而发。只能说遇上是件幸事。
秋招开始捡漏了吗
点赞 评论 收藏
分享
评论
377
100
分享

创作者周榜

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