题解 | #统计复旦用户8月练题情况#
统计复旦用户8月练题情况
https://www.nowcoder.com/practice/53235096538a456b9220fce120c062b3
select u.device_id, u.university, count(q.question_id) as question_cnt, sum(case when q.result='right' then 1 else 0 end) as right_question_cnt # 如果result为right则记为1,其余的无论是wrong还是null全部记为0,然后求和得到的就是正确答题数了 from user_profile u left join question_practice_detail q on u.device_id=q.device_id and month(q.date)=8 # 将表user_profile与question_practice_detail左连接,连接条件为device_id相等且月份为八月 where u.university='复旦大学' # 学校为复旦大学 group by device_id; # 按用户分组