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

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

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

# select device_id, university, count(question_id) question_cnt, right_question_cnt
# from user_profile a 
# left join question_practice_detail b
# on a.device_id = b.device_id
# where university = '复旦大学'
# group by a.device_id

select a.device_id, university, 
case
    when b.question_cnt is null then 0
    else b.question_cnt
end as question_cnt, 
case
    when right_question_cnt is null then 0
    else right_question_cnt
end as right_question_cnt
from user_profile a
left join
(select device_id, count(question_id) question_cnt
from question_practice_detail
where month(date) = 8
group by device_id) as b
on a.device_id = b.device_id
left join
(select device_id, count(result) right_question_cnt
from question_practice_detail
where result = 'right' and month(date) = 8
group by device_id) as c
on a.device_id = c.device_id
where university = '复旦大学';


使用多表连接实现,单独查询出正确答案数和做对题目数,使用左外连接,通过case when函数转换数据中的null值为0.

全部评论

相关推荐

不愿透露姓名的神秘牛友
04-30 18:05
空屿编号:你把墨镜摘下来是不是这样😭
点赞 评论 收藏
分享
04-01 16:02
已编辑
武汉工程大学 Java
牛客98843461...:处女面??我还种马面渣男面处男面呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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