题解 | #统计复旦用户8月练题情况#
统计复旦用户8月练题情况
https://www.nowcoder.com/practice/53235096538a456b9220fce120c062b3
别的都好说,重点是空值的处理,要算一下没有写题目记录的用户数量,根据join后的表 ,所有复旦大学的用户在八月份的答题数量,计算的主键是复旦的设备id,然后如果没有答题数据的话,date和分数啥的就都是空值。
用一个where 筛选出来有时间且是八月的,和一个时间是空值的即可
select a.device_id, a.university, count(b.question_id), sum(if (b.result = 'right',1,0)) from user_profile a left join question_practice_detail b on a.device_id=b.device_id where a.university='复旦大学' and (date_format(b.date,'%Y-%m')='2021-08' OR b.date is null) group by a.device_id,a.university;