题解 | # 计算复旦大学用户8月每天的练题数量,正确数量 #
统计复旦用户8月练题情况
http://www.nowcoder.com/practice/53235096538a456b9220fce120c062b3
筛选目标:
- 复旦大学
- 8月
- 练习数量
- 正确数量
代码示例
select device_id , university,
count(result) as question_cnt,
sum(if(result = 'right',1,0)) as right_question_cnt
from (
select u.*, result, date
from user_profile as u left join question_practice_detail as q
on u.device_id = q.device_id
and extract(month from date) = 8) t
-- 不能用where 会筛选左表的记录 放在内层表示如果不是这个值就记为0
where university = '复旦大学'
-- 在外层做筛选 如果放在内层不会删除其他取值的样本
group by device_id
怎么办,不能光靠直觉啊~
1.放在内外层:
是否保留该变量非特定取值的样本,放在内部保留但取值记为null
放在外层不保留
2.用and还是where
在外层都是where
在内层还是一样 where不保留该变量其他取值