求某时刻最大在线人数

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

相关推荐

如题,求问华为1145和25定律是什么意思?刷到好多人说这个东西了,不知道什么意思
我能加班:如果你在主管面试完当天晚上11点45分收到面试反馈邮件的话,大概率是通过主管面试了。25小时是指在你主管面完成收到短信后的25小时你可以在官网查到你是不是通过。 应该是这样
点赞 评论 收藏
分享
09-05 21:54
已编辑
湖南工程学院 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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