题解 | #统计复旦用户8月练题情况#

https://www.nowcoder.com/practice/53235096538a456b9220fce120c062b3

问题拆解

现在运营想要了解复旦大学的每个用户在8月份练习的总题目数和回答正确的题目数情况,请取出相应明细数据,对于在8月份没有练习过的用户,答题数结果返回0.

1、复旦大学 university="复旦大学"

2、8月份 month(date)=8

3、总题目数 count(question_id)

4、回答正确的题目数 sum(if(result="right",1,0))

完整代码

select
up.device_id,
up.university,
count(qpd.question_id) as question_cnt,
sum(if(qpd.result="right",1,0)) as right_question_cnt
from
user_profile up
left join
question_practice_detail qpd
on up.device_id = qpd.device_id and month(qpd.date) = 8
where
up.university ="复旦大学"
group by 
up.device_id,
up.university

需要注意的细节

1、“对于在8月份没有练习过的用户,答题数结果返回0”,需要将user_profile作为底表,进行左链接 left join

2、对8月进行限制时,需要考虑到,如果month(qpd.date) = 8放在where语句后面,即使是左链接,查询之后返回的结果也是同时满足学校为复旦,且月份为8月的数据,这将会把user_profile表中复旦大学8月没有答题的用户过滤掉

全部评论

相关推荐

酷酷的喜马拉雅山:感觉这比一直在初筛不动的好多了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务