题解 | #每篇文章同一时刻最大在看人数#
每篇文章同一时刻最大在看人数
https://www.nowcoder.com/practice/fe24c93008b84e9592b35faa15755e48
记录一下这种解题思路,学到新东西了。
主要是不知道如何判断一个时间点的同时在线人数,一开始考虑了用单个时间点对每个uid的in_time和on_time都做差看正负,但是这样逻辑有点乱。看了题解的大佬思路,用1和-1代表进入和推出,然后通过时间排序后进行累计加减就能完成了,我得记住哈哈哈
select k.artical_id, max(k.sum_wp) max_uv from ( select *, sum(t.diff) over(partition by t.artical_id order by wtime, t.diff desc) sum_wp from( select artical_id, in_time wtime, 1 diff from tb_user_log union all select artical_id, out_time wtime, -1 diff from tb_user_log ) t where t.artical_id <> 0 ) k group by k.artical_id order by max_uv desc