题意明确:
仅查看山东大学的用户在不同难度下的每个用户的平均答题题目数
问题分解:
- 限定条件:山东大学的用户
up.university="山东大学"
; - 不同难度:按难度分组
group by difficult_level
- 平均答题数:总答题数除以总人数count(qpd.question_id) / count(distinct qpd.device_id) 来自上面信息三个表,需要联表,up与qpd用device_id连接并限定大学,qd与qpd用question_id连接。
细节问题:
- 表头重命名:as
- 平均值精度未明确要求,忽略
完整代码:
select
"山东大学" as university,
difficult_level,
count(qpd.question_id) / count(distinct qpd.device_id) as avg_answer_cnt
from question_practice_detail as qpd
inner join user_profile as up
on up.device_id=qpd.device_id and up.university="山东大学"
inner join question_detail as qd
on qd.question_id=qpd.question_id
group by difficult_level