题解 | #统计复旦用户8月练题情况#
统计复旦用户8月练题情况
https://www.nowcoder.com/practice/53235096538a456b9220fce120c062b3
SELECT p1.device_id,p1.university, COUNT(question_id) AS question_cnt, COUNT(CASE WHEN p2.result = 'right' THEN 1 ELSE NULL END) AS right_question_cnt FROM user_profile AS p1 LEFT JOIN (SELECT * FROM question_practice_detail WHERE MONTH(date) = 8) AS p2 ON p1.device_id = p2.device_id WHERE university = '复旦大学' GROUP BY device_id
要点:
- 表格连结,注意WHERE 8月的限制,因为没有答题记录的用户也要显示,所以8月的限制要放到右边的表格里(LEFT JOIN)
- COUNT 分类统计 ,直接COUNT(result = 'right')是不行的,用CASE WHEN THEN ELSE END 写清楚。
- 本来自己COUNT正确答题数想用子查询,但是失败了,可以继续尝试一下这个思路。