求某时刻最大在线人数

思路:做两个登入登出表,分别是登入时间 和 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
;
#解题#
全部评论

相关推荐

可以不说话:笔试a了3道半,今天说是挂了😭😭
投递汇丰科技等公司8个岗位
点赞 评论 收藏
分享
03-26 22:55
门头沟学院 Java
烤冷面在迎接:河南byd,应该就是郑大了。不过24届计算机是特殊情况,那年除了九✌和强2,以及两三个关系够硬的双非,其他的都是炮灰,感觉是十几年来互联网行业最烂的一年,如果想了解最新的就业情况,得找现在的大四。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务