一张表dwd_user_log 有如下字段:
1)直播间: live_id
2)用户:userid
3)时间戳:date_stamp
4)登陆类型:entry_type (登入in和登出out)
求某天每个直播间最大在线人数?
select live_id,max(total_users) max_total_users
from ( select
live_id
,userid
,date_stamp
,sum(ind) over (partition by live_id order by date_stamp) total_users
from (select
live_id
,userid
,date_stamp
,case when entry_type = ‘in’ then 1
when entry_type = ‘out’ then -1
else 0 end ind
from dwd_user_log
where date_stamp > start_time and date_stamp < end_time
) t1
) t2
group by live_id
;
通过分析dwd_user_log表中直播间live_id、用户userid、时间戳date_stamp和登陆类型entry_type,利用HiveSQL计算指定日期内每个直播间最大的同时在线人数。采用窗口函数sum() over (partition by live_id order by date_stamp)累加登录和登出的标记,最终聚合得到每间直播间的最大在线人数。
4650





