Mysql刷题题解_多表联查_浙江大学的用户在不同难度题目下答题的正确率情况,请取出相应数据,并按照准确率升序输出。

题目详情

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

示例: user_profile

iddevice_idgenderageuniversitygpaactive_days_within_30question_cntanswer_cnt
12138male21北京大学3.47212
23214male复旦大学415525
36543female20北京大学3.212330
42315female23浙江大学3.6512
55432male25山东大学3.8201570
62131male28山东大学3.315713
74321female26复旦大学3.69652

示例: question_practice_detail

iddevice_idquestion_idresult
12138111wrong
23214112wrong
33214113wrong
46543111right
52315115right
62315116right
72315117wrong

示例: question_detail

question_iddifficult_level
111hard
112medium
113easy
115easy
116medium
117easy

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

difficult_levelcorrect_rate
easy0.5000
medium1.0000
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、用户在浙江大学

where unibersity like '浙%

2、统计虽有难度下的答题正确率所以

 sum(CASE
      when result='right' then 1
      else 0
      end))/count(question_id) as correct_rate

3、还要升序排列所以

order by correct_rate asc

4、因为是计算所有不同难度下的题目准确率,所以需要对难度进行分组

group by difficult_level 

之后假设一张已经获取到所有结果的表t,我们可以通过该表t一次查询出我们所需要的内容
可以得到如下表达式

select 
   difficult_level,
   sum( CASE
      when tt.result='right' then 1
      else 0
      end) / count(tt.question_id) as correct_rate
from t 
where university like '浙%'
group by difficult_level order by correct_rate asc;

那我们如何组装这个表t,首先看题中给了三张表,分别记录用户基本信息qu、用户答题情况pu、题目难易程度qs。

分析条件一可知,用户需要时浙江大学的,所以用户基本信息表qu被需要。

分析条件二可知,需要统计所有难度下的答题率,那么我们首先就需要一张存着题目难易程度的用户答题情况表。这个表可以通过pu和qs表内连接得到,命名tt。

由此可以得到我们查询所必须的两张表qu和tt,那么我们把这两张表连接起来不就得到我们表达式中的表t了吗

由此得出答案

select 
 difficult_level,
 sum( CASE
      when tt.result='right' then 1
      else 0
      end) / count(tt.question_id) as correct_rate
from 
user_profile as up join 
   (select qu.*,qd.difficult_level
    from question_practice_detail as qu 
    inner join question_detail as qd on qu.question_id=qd.question_id)
    as tt
    on up.device_id=tt.device_id
where university like '浙%' 
group by difficult_level order by correct_rate asc;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值