题解 | #统计每个用户的平均刷题数#
统计每个用户的平均刷题数
https://www.nowcoder.com/practice/f4714f7529404679b7f8909c96299ac4
select
a.university,
c.difficult_level, --------------------------------------------第五步,由于第四步进行分组,所以select需回应group by条件
round(
count(b.question_id) / count(distinct b.device_id),
4
) as avg_answer_cnt ---------------第六步,由题意知道,平均答题题目数=count(b.question_id) / count(distinct b.device_id),结果保留4位小数【这步是此题的关键点,需要多读题,自行理解怎么得到平均答题题目数】
from
(
select
device_id,
university
from
user_profile
where
university = '山东大学'
) a ---------------------------第一步,先条件求出‘山东大学’
join question_practice_detail b on a.device_id = b.device_id ----------------------第二步,a表 jion b表,关联条件device_id
join question_detail c on b.question_id = c.question_id --------------------------第三步,b表 jion c表,关联条件question_id
group by
a.university,
c.difficult_level; ---------------------------------------------------------------------------------第四步,根据题目要求,对university和difficult_level进行分组
#MySQL#
a.university,
c.difficult_level, --------------------------------------------第五步,由于第四步进行分组,所以select需回应group by条件
round(
count(b.question_id) / count(distinct b.device_id),
4
) as avg_answer_cnt ---------------第六步,由题意知道,平均答题题目数=count(b.question_id) / count(distinct b.device_id),结果保留4位小数【这步是此题的关键点,需要多读题,自行理解怎么得到平均答题题目数】
from
(
select
device_id,
university
from
user_profile
where
university = '山东大学'
) a ---------------------------第一步,先条件求出‘山东大学’
join question_practice_detail b on a.device_id = b.device_id ----------------------第二步,a表 jion b表,关联条件device_id
join question_detail c on b.question_id = c.question_id --------------------------第三步,b表 jion c表,关联条件question_id
group by
a.university,
c.difficult_level; ---------------------------------------------------------------------------------第四步,根据题目要求,对university和difficult_level进行分组
#MySQL#