题解 | 统计复旦用户8月练题情况 (sum(if)函数和对null的处理)

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

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

select

t1.device_id,

t1.university,

count(t2.device_id) as question_cnt,

sum(if(t2.result='right',1,0)) as right_question_cnt

from

user_profile t1

left join

question_practice_detail t2

on t1.device_id=t2.device_id and month(t2.date)=8

where t1.university='复旦大学'

group by t1.device_id,t1.university

--因为用到了查询字段和聚合函数在一起,所以必须用group by

--month的判断必须放在on的位置,因为只有这样才会有null,count的时候自动会数成0

全部评论

相关推荐

05-03 12:45
西南大学 Java
nsnzkv:你这项目写的内容太多了,说实话都是在给自己挖坑,就算简历过了,后面面试也难受
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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