题解 | #每篇文章同一时刻最大在看人数#
每篇文章同一时刻最大在看人数
http://www.nowcoder.com/practice/fe24c93008b84e9592b35faa15755e48
自己写的错误案例: 所有情况都暂时考虑到,但是忽略了如果处于同时刻则先加一后减一的情况,导致在sql里和自测答案正确,提交不通过……所有这个条件应该放在哪呢~
select artical_id,
max(在读人数) as 最大同时在读人数
from
(select *,
sum(动作标识) over(partition by artical_id order by 时间标识) as 在读人数 from
((select artical_id,in_time as 时间标识,1 as 动作标识 from tb_user_log where artical_id <> 0)
union all
(select artical_id,out_time as 时间标识,-1 as 动作标识 from tb_user_log where artical_id <> 0)) a) b
group by artical_id
order by 最大同时在读人数 desc;