第三十一例:综合练习,统计复旦用户8月练题情况
- 题目地址:统计复旦用户8月练题情况牛客题霸牛客网 (nowcoder.com)
- 初始化数据:
droptable if exists `user_profile`;
droptable if exists `question_practice_detail`;
droptable if exists `question_detail`;
CREATETABLE `user_profile` (
`id` intNOTNULL,
`device_id` intNOTNULL,
`gender` varchar(14) NOTNULL,
`age` int ,
`university` varchar(32) NOTNULL,
`gpa` float,
`active_days_within_30` int ,
`question_cnt` int ,
`answer_cnt` int
);
CREATETABLE `question_practice_detail` (
`id` intNOTNULL,
`device_id` intNOTNULL,
`question_id`int NOTNULL,
`result` varchar(32) NOTNULL,
`date` dateNOTNULL
);
CREATETABLE `question_detail` (
`id` intNOTNULL,
`question_id`int NOTNULL,
`difficult_level` varchar(32) NOTNULL
);
INSERTINTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
INSERTINTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
INSERTINTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
INSERTINTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
INSERTINTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
INSERTINTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
INSERTINTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
INSERTINTO question_practice_detail VALUES(1,2138,111,'wrong','2021-05-03');
INSERTINTO question_practice_detail VALUES(2,3214,112,'wrong','2021-05-09');
INSERTINTO question_practice_detail VALUES(3,3214,113,'wrong','2021-06-15');
INSERTINTO question_practice_detail VALUES(4,6543,111,'right','2021-08-13');
INSERTINTO question_practice_detail VALUES(5,2315,115,'right','2021-08-13');
INSERTINTO question_practice_detail VALUES(6,2315,116,'right','2021-08-14');
INSERTINTO question_practice_detail VALUES(7,2315,117,'wrong','2021-08-15');
INSERTINTO question_practice_detail VALUES(8,3214,112,'wrong','2021-05-09');
INSERTINTO question_practice_detail VALUES(9,3214,113,'wrong','2021-08-15');
INSERTINTO question_practice_detail VALUES(10,6543,111,'right','2021-08-13');
INSERTINTO question_practice_detail VALUES(11,2315,115,'right','2021-08-13');
INSERTINTO question_practice_detail VALUES(12,2315,116,'right','2021-08-14');
INSERTINTO question_practice_detail VALUES(13,2315,117,'wrong','2021-08-15');
INSERTINTO question_practice_detail VALUES(14,3214,112,'wrong','2021-08-16');
INSERTINTO question_practice_detail VALUES(15,3214,113,'wrong','2021-08-18');
INSERTINTO question_practice_detail VALUES(16,6543,111,'right','2021-08-13');
INSERTINTO question_detail VALUES(1,111,'hard');
INSERTINTO question_detail VALUES(2,112,'medium');
INSERTINTO question_detail VALUES(3,113,'easy');
INSERTINTO question_detail VALUES(4,115,'easy');
INSERTINTO question_detail VALUES(5,116,'medium');
INSERTINTO question_detail VALUES(6,117,'easy');
- 题目描述:想要了解复旦大学的每个用户在8月份练习的总题目数和回答正确的题目数情况,请取出相应明细数据,对于在8月份没有练习过的用户,答题数结果返回0
- 分析1:先统计每个用户在8月份练习的总题目数和回答正确的题目数:
select
up.device_id,
up.university
from
user_profile up
left join
question_practice_detail qpd
on
up.device_id=qpd.device_id
and
month(qpd.date) = '08'andyear(qpd.date) = '2021'
where
up.university='复旦大学';
- 分析2:接下来统计答题的总数量和答题正确的数量,8月份没有练习过的用户,答题结果返回0
条件表达式语法:
case
when sex='1' then
'男'
when sex='2' then '女'
else '其他'
end
sum(
case
when qpd.resultisnotnull
then 1
else 0
end
) as question_cnt,
sum(
case
when qpd.result = 'right'
then 1
else 0
end
) as right_question_cnt
- 代码示例:
select
up.device_id,
up.university,
sum(
case
when qpd.resultisnotnull
then 1
else 0
end
) as question_cnt,
sum(
case
when qpd.result = 'right'
then 1
else 0
end
) as right_question_cnt
from
user_profile up
left join
question_practice_detail qpd
on
up.device_id=qpd.device_id
and
month(qpd.date) = '08'andyear(qpd.date) = '2021'
where
up.university='复旦大学'
groupby
device_id;
第三十二题:综合练习,浙大不同难度题目的正确率
- 题目地址:浙大不同难度题目的正确率牛客题霸牛客网 (nowcoder.com)
- 初始化数据:
droptable if exists `user_profile`;
droptable if exists `question_practice_detail`;
droptable if exists `question_detail`;
CREATETABLE `user_profile` (
`id` intNOTNULL,
`device_id` intNOTNULL,
`gender` varchar(14) NOTNULL,
`age` int ,
`university` varchar(32) NOTNULL,
`gpa` float,
`active_days_within_30` int ,
`question_cnt` int ,
`answer_cnt` int
);
CREATETABLE `question_practice_detail` (
`id` intNOTNULL,
`device_id` intNOTNULL,
`question_id`int NOTNULL,
`result` varchar(32) NOTNULL,
`date` dateNOTNULL
);
CREATETABLE `question_detail` (
`id` intNOTNULL,
`question_id`int NOTNULL,
`difficult_level` varchar(32) NOTNULL
);
INSERTINTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
INSERTINTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
INSERTINTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
INSERTINTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
INSERTINTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
INSERTINTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
INSERTINTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
INSERTINTO question_practice_detail VALUES(1,2138,111,'wrong','2021-05-03');
INSERTINTO question_practice_detail VALUES(2,3214,112,'wrong','2021-05-09');
INSERTINTO question_practice_detail VALUES(3,3214,113,'wrong','2021-06-15');
INSERTINTO question_practice_detail VALUES(4,6543,111,'right','2021-08-13');
INSERTINTO question_practice_detail VALUES(5,2315,115,'right','2021-08-13');
INSERTINTO question_practice_detail VALUES(6,2315,116,'right','2021-08-14');
INSERTINTO question_practice_detail VALUES(7,2315,117,'wrong','2021-08-15');
INSERTINTO question_practice_detail VALUES(8,3214,112,'wrong','2021-05-09');
INSERTINTO question_practice_detail VALUES(9,3214,113,'wrong','2021-08-15');
INSERTINTO question_practice_detail VALUES(10,6543,111,'right','2021-08-13');
INSERTINTO question_practice_detail VALUES(11,2315,115,'right','2021-08-13');
INSERTINTO question_practice_detail VALUES(12,2315,116,'right','2021-08-14');
INSERTINTO question_practice_detail VALUES(13,2315,117,'wrong','2021-08-15');
INSERTINTO question_practice_detail VALUES(14,3214,112,'wrong','2021-08-16');
INSERTINTO question_practice_detail VALUES(15,3214,113,'wrong','2021-08-18');
INSERTINTO question_practice_detail VALUES(16,6543,111,'right','2021-08-13');
INSERTINTO question_detail VALUES(1,111,'hard');
INSERTINTO question_detail VALUES(2,112,'medium');
INSERTINTO question_detail VALUES(3,113,'easy');
INSERTINTO question_detail VALUES(4,115,'easy');
INSERTINTO question_detail VALUES(5,116,'medium');
INSERTINTO question_detail VALUES(6,117,'easy');
- 题目描述:想要了解浙江大学的用户在不同难度题目下答题的正确情况,按照准确率升序输出
- 分析1:先找出浙江大学的用户有多少:
select
up.device_id
from
user_profile up
where
university='浙江大学';
- 分析2:查看浙江大学答题情况:
select
up.device_id,qpd.result
from
user_profile up
inner join
question_practice_detail qpd
on
up.device_id = qpd.device_id
where
up.university = '浙江大学';
- 分析3:找出答题题目对于的难度:
select
up.device_id,qpd.result,qd.diffcult_level
from
user_profile up
inner join
question_practice_detail qpd
on
up.device_id = qpd.device_id
inner join
question_detail qd
on
qpd.question_id = qd.question_id
where
up.university = '浙江大学';
- 分析4:统计答题的正确率情况
sum(
case
when qpd.result='right'
then 1
else 0
end
) / count(qpd.result) as correct_rate
- 代码示例:
select
qd.difficult_level,
sum(
case
when qpd.result='right'
then 1
else 0
end
) / count(qpd.result) as correct_rate
from
user_profile up
inner join
question_practice_detail qpd
on
up.device_id = qpd.device_id
inner join
question_detail qd
on
qpd.question_id = qd.question_id
where
up.university = '浙江大学'
groupby
qd.difficult_level
orderby
correct_rate asc;
第三十三例:综合练习,21年8月份练题总数
- 题目地址:21年8月份练题总数牛客题霸牛客网 (nowcoder.com)
- 初始化数据:
droptable if exists `user_profile`;
droptable if exists `question_practice_detail`;
CREATETABLE `user_profile` (
`id` intNOTNULL,
`device_id` intNOTNULL,
`gender` varchar(14) NOTNULL,
`age` int ,
`university` varchar(32) NOTNULL,
`gpa` float,
`active_days_within_30` int ,
`question_cnt` int ,
`answer_cnt` int
);
CREATETABLE `question_practice_detail` (
`id` intNOTNULL,
`device_id` intNOTNULL,
`question_id`int NOTNULL,
`result` varchar(32) NOTNULL,
`date` dateNOTNULL
);
INSERTINTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
INSERTINTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
INSERTINTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
INSERTINTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
INSERTINTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
INSERTINTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
INSERTINTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
INSERTINTO question_practice_detail VALUES(1,2138,111,'wrong','2021-05-03');
INSERTINTO question_practice_detail VALUES(2,3214,112,'wrong','2021-05-09');
INSERTINTO question_practice_detail VALUES(3,3214,113,'wrong','2021-06-15');
INSERTINTO question_practice_detail VALUES(4,6543,111,'right','2021-08-13');
INSERTINTO question_practice_detail VALUES(5,2315,115,'right','2021-08-13');
INSERTINTO question_practice_detail VALUES(6,2315,116,'right','2021-08-14');
INSERTINTO question_practice_detail VALUES(7,2315,117,'wrong','2021-08-15');
INSERTINTO question_practice_detail VALUES(8,3214,112,'wrong','2021-05-09');
INSERTINTO question_practice_detail VALUES(9,3214,113,'wrong','2021-08-15');
INSERTINTO question_practice_detail VALUES(10,6543,111,'right','2021-08-13');
INSERTINTO question_practice_detail VALUES(11,2315,115,'right','2021-08-13');
INSERTINTO question_practice_detail VALUES(12,2315,116,'right','2021-08-14');
INSERTINTO question_practice_detail VALUES(13,2315,117,'wrong','2021-08-15');
INSERTINTO question_practice_detail VALUES(14,3214,112,'wrong','2021-08-16');
INSERTINTO question_practice_detail VALUES(15,3214,113,'wrong','2021-08-18');
INSERTINTO question_practice_detail VALUES(16,6543,111,'right','2021-08-13');
- 题目描述:想要了解2021年8月份所有练习过题目的总用户数和练习过的总次数
- 分析1:找出练习过题目的总用户数:
select
count(distinct device_id) as did_cnt
from
question_practice_detail
where
datelike'2021-08%';
- 代码示例
select
count(distinct device_id) as did_cnt,
count(question_id) as question_cnt
from
question_practice_detail
where
datelike'2021-08%';
- 代码示例2:
select
count(distinct device_id) as did_cnt,
count(question_id) as question_cnt
from
question_practice_detail
where
date>='2021-08-01'anddate <= '2021-08-31';
- 代码示例3:
select
count(distinct device_id) as did_cnt,
count(question_id) as question_cnt
from
question_practice_detail
where
year(date)=2021and month(date)=8;