题解 | #浙大不同难度题目的正确率#
浙大不同难度题目的正确率
https://www.nowcoder.com/practice/d8a4f7b1ded04948b5435a45f03ead8c
select c.difficult_level, sum(b.result = 'right')/count(b.question_id) as correct_rate from user_profile a inner join question_practice_detail b on a.device_id = b.device_id and a.university = '浙江大学' left join question_detail c on b.question_id = c.question_id group by c.difficult_level order by correct_rate
本题统计不同等级的题目下浙大学生的正确率,因此对于浙大学生未答题的情况需要忽略,所以user_profile和question_practice_detail是内连接,取交集;而非左连接,避免出现question_id为空的数据。
随后,再连接question_detail根据difficult_level分组,计算正确率:
正确率 = 组内result正确的计数/组内问题计数,有三种写法:
(1) avg:此时正确率即为计算正确的平均值
avg(if(result='right',1,0))
(2) sum +if:组内正确的赋值为1,再求和
sum(if(result='right',1,0))/count(*)
(3)count+if:将组内正确的赋值为1,不正确的赋值为空,这样count才不会计数不正确的行数据。
count(if(result='right',1,null))/count(*)