题解 | SQL24 统计每个用户的平均刷题数
这题和上题类似,但是考察了where
和having
的区别。
where
是要在group by
的前面,而having
是出现在group by
的后面。所以我们这里只能用having
跟在group by
的后面。
select
university,
difficult_level,
count(q.question_id) / count(distinct q.device_id) as 'avg_answer_cnt'
from
user_profile as u
join question_practice_detail as q
on u.device_id = q.device_id
join question_detail as qd
on q.question_id = qd.question_id
group by university, difficult_level
having university = '山东大学';