题解 | #统计复旦用户8月练题情况#
统计复旦用户8月练题情况
http://www.nowcoder.com/practice/53235096538a456b9220fce120c062b3
题目: 现在运营想要了解复旦大学的每个用户在8月份练习的总题目数和回答正确的题目数情况,请取出相应明细数据,对于在8月份没有练习过的用户,答题数结果返回0.
思考步骤:
- 复旦大学,应该会用到where
- 8月, month(date)=8
- 总题目数:对问题id进行计数
- 正确的题目 可以求和,求和之前做一次筛选。如下:
思路一:
if(qpd.result='right',1,0)
思路二:
case when qpd.result='right' then 1
else 0 end
易错点:
8月份没有练习过的用户
多数人会在where处限制8月份的,结果越少没有练习的用户
正确处理:
把限制条件在where之前,比如在链接的时候弄上
完整答案:
select up.device_id,
up.university,
COUNT(question_id) question_cnt,
sum(if(qpd.result='right',1,0)) right_question_cnt
from
user_profile up left join question_practice_detail qpd on
up.device_id=qpd.device_id and month(qpd.date)=08
where
university = '复旦大学'
GROUP by up.device_id