题解 | #每篇文章同一时刻最大在看人数#
每篇文章同一时刻最大在看人数
https://www.nowcoder.com/practice/fe24c93008b84e9592b35faa15755e48
select artical_id,max(m) max_uv from( select artical_id,t, sum(type) over (partition by artical_id order by t,type desc ) m from( select artical_id,in_time t ,1 type from tb_user_log where artical_id != 0 union all select artical_id,out_time t ,-1 type from tb_user_log where artical_id != 0 ) t1 ) t2 group by artical_id order by max_uv desc
流程:
一、artical_id和进入时间创建一个表,并且加入一列数字1表示浏览状态
artical_id和进入登出创建一个表,并且加入一列数字-1表示浏览状态
将两张表通过unino all连接
二、使用窗口函数,按id分组时间排序,求出每个时刻的人数,最后去最大值