求某时刻最大在线人数
思路:做两个登入登出表,分别是登入时间 和 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
;
#解题#
然后用窗口 求窗口大小为( 上无限到当前位置的, 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
;
#解题#
全部评论
相关推荐
11-12 18:47
中南大学 Java 点赞 评论 收藏
分享
求offer的社畜很想吃卤蛋:篮子对快手是不满意吗
点赞 评论 收藏
分享