题解 | 先升序再降序,可以排除同时间点进出|每篇文章同一时刻最大在看人数
每篇文章同一时刻最大在看人数
https://www.nowcoder.com/practice/fe24c93008b84e9592b35faa15755e48
with raw as(
select
id,
uid,
artical_id,
in_time as time,
1 as tag
from tb_user_log
where artical_id != 0
union
select
id,
uid,
artical_id,
out_time as time,
-1 as tag
from tb_user_log
where artical_id != 0
)
select
artical_id,
max(maxnum) as max_uv
from
(
select
artical_id,
tag,
sum(tag)over(partition by artical_id order by time asc,tag desc) as maxnum
from raw
)t
group by 1
order by max_uv desc
