求某时刻最大在线人数

思路:做两个登入登出表,分别是登入时间 和 1, 登出时间 -1 用union all合并(因为可能有人是同时登入) , 状态字段用status表示
然后用窗口  求窗口大小为( 上无限到当前位置的,  status的sum() ),即可求出, 某时刻最大在线人数

with t1 as
(select artical_id,in_time time ,
       1 as status
from tb_user_log
where artical_id!=0
union all
select artical_id,out_time time ,
       -1 as status
from tb_user_log
    where artical_id!=0)
, t2 as
(select artical_id,
       sum(status)over(partition by artical_id order by time rows between unbounded preceding and current row ) cnt
from t1
)
select artical_id,max(cnt) max_uv
from t2
group by artical_id
order by max_uv desc
;
#解题#
全部评论

相关推荐

嗨害嗨我来了:你跟他说开迈巴赫呢,一个月好几万,让学弟尝尝一点小小的社会险恶
点赞 评论 收藏
分享
07-28 16:10
门头沟学院 Java
连笔试都没有就直接挂了 这是学历厂吗两段大厂实习一段中厂一点机会都没有吗真的很难绷
xiaolihuam...:校招挂了,然后反手给我捞了个社招
投递虾皮信息等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务