大数据面试SQL每日一题系列:最高峰同时在线主播人数。字节,快手等大厂高频面试题

之后会不定期更新每日一题sql系列。

SQL面试题每日一题系列内容均来自于网络以及实际使用情况收集,如有雷同,纯属巧合。

1.题目

**问题1:**如下为某直播平台各主播的开播及关播时间数据明细,现在需要计算该平台最高峰期同时在线的主播人数。

问题2:以下为某直播间用户上线与下线的时间数据明细,现求该直播间最高峰同时在线的用户人数。

以上两个问法为同一问题。

2.基础数据准备

create table if not exists temp.user_login_info (
  `id` bigint comment '用户id',
  `start_time` string comment '上线时间',
  `end_time` string comment '下线时间'
) comment '用户上下线时间测试'

数据预览

idstart_timeend_time
12024-05-05 07:59:062024-05-05 08:57:54
22024-05-05 08:14:022024-05-05 08:51:32
32024-05-05 08:38:102024-05-05 08:38:28
42024-05-05 08:41:222024-05-05 08:42:03
52024-05-05 08:33:392024-05-05 08:52:19
62024-05-05 08:54:502024-05-05 08:56:07
72024-05-05 08:56:122024-05-05 08:57:42
82024-05-05 08:21:432024-05-05 08:21:48
92024-05-05 07:59:582024-05-05 08:13:42
102024-05-05 08:20:052024-05-05 08:29:42

3.问题分析

查询同时最大人数,考察的是对拉链转化为日志的处理方式以及聚合开窗函数的累积计算的使用。聚合开窗函数使用详见SQL窗口函数之聚合函数类

维度评分
题目难度⭐️⭐️⭐️⭐️
题目清晰度⭐️⭐️⭐️⭐️⭐️
业务常见度⭐️⭐️⭐️⭐️⭐️

4.解题SQL

1.生成日志流水

对原始数据进行处理,生成主播上下线的日志流水数据,增加标记状态值(上线为1,下线为-1)。

-- 上播记录
select
id,
start_time as log_time,
1 as flag
from temp.user_login_info
union all 
-- 下播记录
select
id,
end_time as log_time,
-1 as flag
from temp.user_login_info

数据结果如下:

idlog_timeflag
12024-05-05 08:57:54-1
22024-05-05 08:51:32-1
32024-05-05 08:38:28-1
42024-05-05 08:42:03-1
52024-05-05 08:52:19-1
62024-05-05 08:56:07-1
72024-05-05 08:57:42-1
82024-05-05 08:21:48-1
92024-05-05 08:13:42-1
102024-05-05 08:29:42-1
12024-05-05 07:59:061
22024-05-05 08:14:021
32024-05-05 08:38:101
42024-05-05 08:41:221
52024-05-05 08:33:391
62024-05-05 08:54:501
72024-05-05 08:56:121
82024-05-05 08:21:431
92024-05-05 07:59:581
102024-05-05 08:20:051

2.开窗函数聚合

对上下线日志流水进行开窗聚合累积计算且查看上下线明细。

select id,log_time,flag,sum(flag) over(order by log_time) as acum_login from (
  -- 上播记录
  select
  id,
  start_time as log_time,
  1 as flag
  from temp.user_login_info where id <= 10
  union all 
  -- 下播记录
  select
  id,
  end_time as log_time,
  -1 as flag
  from temp.user_login_info where id <= 10
) a
order by log_time

数据结果

idlog_timeflagacum_login
12024-05-05 07:59:0611
92024-05-05 07:59:5812
92024-05-05 08:13:42-11
22024-05-05 08:14:0212
102024-05-05 08:20:0513
82024-05-05 08:21:4314
82024-05-05 08:21:48-13
102024-05-05 08:29:42-12
52024-05-05 08:33:3913
32024-05-05 08:38:1014
32024-05-05 08:38:28-13
42024-05-05 08:41:2214
42024-05-05 08:42:03-13
22024-05-05 08:51:32-12
52024-05-05 08:52:19-11
62024-05-05 08:54:5012
62024-05-05 08:56:07-11
72024-05-05 08:56:1212
72024-05-05 08:57:42-11
12024-05-05 08:57:54-10

3.计算最大在线人数

最后计算最大同时在线人数

select max(acum_login) as max_acum_login from (
  select id,log_time,flag,sum(flag) over(order by log_time) as acum_login from (
    select
    id,
    start_time as log_time,
    1 as flag
    from temp.user_login_info where id <= 10
    union all 
    --下播记录
    select
    id,
    end_time as log_time,
    -1 as flag
    from temp.user_login_info where id <= 10
  ) a
) b 

数据结果

max_acum_login
4

最大在线人数为4。

5.衍生问题解答

如果是最上面的问题2,每个房间同时在线最大人数呢?

那它的写法应该是这样的。

select room_id,max(acum_login) as max_acum_login from (
    select id,room_id
  		,log_time,flag
  		,sum(flag) over(partition by room_id order by log_time) as acum_login 
  	from (
        -- 上线记录
      	select
        id,room_id,
        start_time as log_time,
        1 as flag
        from temp.user_login_info where id <= 10
        union all 
        -- 下线记录
        select
        id,room_id,
        end_time as log_time,
        -1 as flag
        from temp.user_login_info where id <= 10
    ) a
) b 
group by room_id

就不补充具体数据演示了。

思路:以第一个问题为基础,这里只是多增加了一个房间维度,按房间分组进行开窗聚合累积计算以及最后的分组求最大值。如有问题,欢迎联系我点击此处加群一起学习讨论。

以上,本期全部内容。

感谢阅读。

按例,欢迎点击此处关注我的个人公众号,交流更多知识。

  • 18
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鲁边

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值