题解 | #统计复旦用户8月练题情况#
统计复旦用户8月练题情况
https://www.nowcoder.com/practice/53235096538a456b9220fce120c062b3
select up.device_id,
university,
count(case when date between '2021-08-01' and '2021-08-31' then qpd.id else null end) as question_cnt, -- 只计算8月内的题目
count(case when date between '2021-08-01' and '2021-08-31' and result = 'right' then qpd.id else null end) as right_question_cnt -- 只计算8月内做对的题目
from (
select device_id,
university
from user_profile
where university = '复旦大学'
) up
left join question_practice_detail qpd -- 如果希望保留为0的条目,则需要用大表联小表;反向right join将会导致一堆null的条目,在计数的时候不会被统计
on up.device_id = qpd.device_id
-- “8月内”这个时间条件不能用在最外层,否则会把其他月份做题数量为0的记录筛选掉
group by up.device_id
SHEIN希音公司福利 283人发布
