求某时刻最大在线人数
思路:做两个登入登出表,分别是登入时间 和 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
;
#解题#
全部评论
相关推荐
12-09 15:17
哈尔滨理工大学 后端工程师 点赞 评论 收藏
分享
点赞 评论 收藏
分享
专业嗎喽:硕佬,把学校背景放后面几段,学校背景双非还学院,让人看了就不想往下看。
把实习经历和个人奖项放前面,用数字化简述自己实习的成果和掌握的技能,比如负责项目一次通过率90%,曾4次发现项目潜在问题风险为公司减少损失等等 点赞 评论 收藏
分享
10-15 10:23
门头沟学院 Java
牛可乐的头像真牛:赶紧举报,这公司绝对是诈骗的,等你签约后工作一两个月后根据合同漏洞把你开除,并且要求你赔偿3w培训费,996是为了提前筛选心甘情愿签下合同容易受骗的群体,纯粹面向校招生精心设计的骗局 点赞 评论 收藏
分享
