live_max_online
305 字
2 分钟
live_max_online
-- step0 建表-- drop table cm.tb_live_logs;create table cm.tb_live_logs( live_id int, user_id int, ts datetime, type varchar(10));
-- step1 插入数据insert into cm.tb_live_logs(live_id, user_id, ts, type) values(901,1001,'2022-10-01 12:00:00','IN'),(901,1002,'2022-10-01 12:01:00','IN'),(901,1003,'2022-10-01 12:01:00','IN'),(901,1004,'2022-10-01 12:02:00','IN'),(901,1005,'2022-10-01 12:02:00','IN'),(901,1006,'2022-10-01 12:03:00','IN'),(901,1007,'2022-10-01 12:03:00','IN'),(901,1008,'2022-10-01 12:05:00','IN'),(901,1009,'2022-10-01 12:05:00','IN'),(901,1010,'2022-10-01 12:06:03','IN'),(902,1101,'2022-10-01 12:00:00','IN'),(902,1102,'2022-10-01 12:01:00','IN'),(902,1103,'2022-10-01 12:01:00','IN'),(902,1104,'2022-10-01 12:02:00','IN'),(902,1105,'2022-10-01 12:29:00','IN'),(902,1106,'2022-10-01 12:30:00','IN'),(902,1107,'2022-10-01 12:31:00','IN'),(902,1108,'2022-10-01 12:32:00','IN'),(902,1109,'2022-10-01 12:39:00','IN'),(902,1110,'2022-10-01 12:06:03','IN'),(901,1001,'2022-10-01 12:03:03','OUT'),(901,1002,'2022-10-01 12:01:00','OUT'),(901,1003,'2022-10-01 12:03:03','OUT'),(901,1004,'2022-10-01 12:05:03','OUT'),(901,1005,'2022-10-01 12:10:03','OUT'),(901,1006,'2022-10-01 12:03:01','OUT'),(901,1007,'2022-10-01 12:03:03','OUT'),(901,1008,'2022-10-01 12:06:12','OUT'),(901,1009,'2022-10-01 12:06:03','OUT'),(901,1010,'2022-10-01 12:10:03','OUT'),(902,1101,'2022-10-01 12:03:03','OUT'),(902,1102,'2022-10-01 12:03:03','OUT'),(902,1103,'2022-10-01 12:03:03','OUT'),(902,1104,'2022-10-01 12:05:03','OUT'),(902,1105,'2022-10-01 12:30:03','OUT'),(902,1106,'2022-10-01 12:30:01','OUT'),(902,1107,'2022-10-01 12:40:03','OUT'),(902,1108,'2022-10-01 12:44:12','OUT'),(902,1109,'2022-10-01 12:42:03','OUT'),(902,1110,'2022-10-01 12:10:03','OUT');
-- step2 IN/OUT转数字select live_id, user_id, ts , IF(type = 'IN', 1, -1) as contributionfrom cm.tb_live_logs;
-- step3 按照时间进行累加,求每个时间点的deltaselect live_id, ts, sum(contribution) as new_numberfrom (select live_id, user_id, ts, IF(type = 'IN', 1, -1) as contribution from cm.tb_live_logs ) t1group by live_id, ts;
-- step4 按照时间顺序累加select *, sum(new_number) over (partition by live_id order by ts) as online_numberfrom (select live_id, ts, sum(contribution) as new_number from (select live_id, user_id, ts, IF(type = 'IN', 1, -1) as contribution from cm.tb_live_logs ) t1 group by live_id, ts ) t2;
-- step5 求累加过程中,最大的值,就是同一时刻最大在线用户select live_id, max(online_number) as max_online_numberfrom (select *, sum(new_number) over (partition by live_id order by ts) as online_number from (select live_id, ts, sum(contribution) as new_number from (select live_id, user_id, ts, IF(type = 'IN', 1, -1) as contribution from cm.tb_live_logs ) t1 group by live_id, ts ) t2 ) t3group by live_id;支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!
live_max_online
https://fanrich.eu.org/posts/程技/sql/live_max_online/相关文章智能推荐
1
MySQL常用函数
程技2023-09-19
2
给定用户登录表,求表中每一天的3天留存率
程技2023-08-01
3
求占据前90%销售额的商品类型
程技2023-08-01
4
MySQL常用30种SQL查询语句优化方法
程技2023-08-01
5
查询连续登陆3天的用户id和登陆天数
程技2023-08-01
随机文章随机推荐
樊笼










