ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2 浙大不同难度题目的正确性

2022-01-15 01:32:13  阅读:26  来源: 互联网

标签:INSERT 题目 INTO practice question detail 浙大 正确性 VALUES


目录

2 浙大不同难度题目的正确性

题目

题目:现在运营想要了解浙江大学的用户在不同难度题目下答题的正确率情况,请取出相应数据,并按照准确率升序输出。

示例: user_profile
image

示例: question_practice_detail
image

示例: question_detail
image

根据示例,你的查询应返回以下结果:
image

示例1

# 输入
drop table if exists `user_profile`;
drop table if  exists `question_practice_detail`;
drop table if  exists `question_detail`;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`gpa` float,
`active_days_within_30` int ,
`question_cnt` int ,
`answer_cnt` int 
);
CREATE TABLE `question_practice_detail` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`question_id`int NOT NULL,
`result` varchar(32) NOT NULL,
`date` date NOT NULL
);
CREATE TABLE `question_detail` (
`id` int NOT NULL,
`question_id`int NOT NULL,
`difficult_level` varchar(32) NOT NULL
);

INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong','2021-05-03');
INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong','2021-05-09');
INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong','2021-06-15');
INSERT INTO question_practice_detail VALUES(4,6543,111,'right','2021-08-13');
INSERT INTO question_practice_detail VALUES(5,2315,115,'right','2021-08-13');
INSERT INTO question_practice_detail VALUES(6,2315,116,'right','2021-08-14');
INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong','2021-08-15');
INSERT INTO question_practice_detail VALUES(8,3214,112,'wrong','2021-05-09');
INSERT INTO question_practice_detail VALUES(9,3214,113,'wrong','2021-08-15');
INSERT INTO question_practice_detail VALUES(10,6543,111,'right','2021-08-13');
INSERT INTO question_practice_detail VALUES(11,2315,115,'right','2021-08-13');
INSERT INTO question_practice_detail VALUES(12,2315,116,'right','2021-08-14');
INSERT INTO question_practice_detail VALUES(13,2315,117,'wrong','2021-08-15');
INSERT INTO question_practice_detail VALUES(14,3214,112,'wrong','2021-08-16');
INSERT INTO question_practice_detail VALUES(15,3214,113,'wrong','2021-08-18');
INSERT INTO question_practice_detail VALUES(16,6543,111,'right','2021-08-13');
INSERT INTO question_detail VALUES(1,111,'hard');
INSERT INTO question_detail VALUES(2,112,'medium');
INSERT INTO question_detail VALUES(3,113,'easy');
INSERT INTO question_detail VALUES(4,115,'easy');
INSERT INTO question_detail VALUES(5,116,'medium');
INSERT INTO question_detail VALUES(6,117,'easy');

#输出

easy|0.5000
medium|1.0000

题解

分析
1,题目的正确性 = 正确的题目数/一共做了的题目数

思路1
1.浙大的同学做了哪些题目
2.这些题目根据难度分类,统计每种难度的题目总数
3.根据难度统计题目正确的数目

SELECT qd.difficult_level,SUM(IF(a.result='right',1,0))/count(question_id)  AS correct_rate -- IF(a.result='right',1,0)表示给每一行添加一个列,如果a.result='right'则该列的值为1,反之该列的值为0
-- 可替换成AVG(IF(result='right',1,0)) = 总和数/行数
FROM(
    SELECT qpd.question_id,qpd.result
    FROM question_practice_detail qpd
    WHERE qpd.device_id IN (-- 浙江大学的用户是2315
        SELECT u.device_id
        FROM user_profile u
        WHERE u.university='浙江大学'
    ) -- 从question_practice_detail中挑选出浙江大学用户2315的答题id,及对应的结果
)a JOIN  question_detail qd USING(question_id) -- 题目和难度记录在一起
GROUP BY qd.difficult_level -- 根据难度分组
ORDER BY correct_rate  -- 根据准确度降序

思路2
直接三个表连接在一起,筛选出浙江大学的行,并按照题目难度分类

SELECT difficult_level,
AVG(IF(result='right',1,0)) AS correct_rate-- 这里AVG(IF(result='right',1,0)) = 总和数/行数
FROM user_profile u, question_practice_detail qpd, question_detail qd
WHERE u.device_id = qpd.device_id AND qpd.question_id = qd.question_id
AND university='浙江大学'
GROUP BY difficult_level
ORDER BY correct_rate;

标签:INSERT,题目,INTO,practice,question,detail,浙大,正确性,VALUES
来源: https://www.cnblogs.com/rananie/p/15805994.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有