题解 | 统计复旦用户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.

全部评论

相关推荐

05-04 17:20
武汉大学
已注销:技术栈删了,让ai把你的项目丰富化,干的活太少了,像是写了个demo,起码一个项目四点重要内容,内容用技术栈描述,取得了什么进展,简历大部分都留给项目,其他的压缩
如何写一份好简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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