【开窗】取最近直播标题和直播时长

问题描述:

一张表:test_over(uid, title, stime, etime, date)

字段comment:主播ID,直播间标题,开始直播时间戳,结束直播时间戳,日期分区

问题:求每个主播当天最近一次直播记录和全天总的直播时长

要求: shuffle次数最少


思路:

粒度是主播;时间周期是每天;需求是两个,第一,当天最近一次直播记录,第二全天总的直播时长;要求shuffle次数最低;


数据准备:

数据准备

create table test_over(

uid int comment '主播ID'

,title varchar(50) comment '直播间标题'

,stime int comment'开始直播时间戳'

,etime int comment'结束直播时间戳'

,mdate date comment'日期分区'

);

 

insert overwrite table test_over

select 1,'直播1',1591607704,1591609704,'2020-06-08'

union all

select 1,'直播2',1591617204,1591621204,'2020-06-08'

union all

select 1,'直播3',1591626204,1591628204,'2020-06-08'

union all

select 1,'直播4',1591628304,1591629304,'2020-06-08'

union all

select 2,'直播5',1591607604,1591609588,'2020-06-08'

union all

select 2,'直播6',1591617208,1591621206,'2020-06-08'

union all

select 2,'直播7',1591626208,1591628206,'2020-06-08'

union all

select 2,'直播8',1591628308,1591629306,'2020-06-08'

union all

select 3,'直播11',1591607704,1591609704,'2020-06-09'

union all

select 3,'直播12',1591617204,1591621204,'2020-06-09'

union all

select 3,'直播13',1591626204,1591628204,'2020-06-09'

union all

select 3,'直播14',1591628304,1591629304,'2020-06-09'

union all

select 4,'直播15',1591607604,1591609588,'2020-06-09'

union all

select 4,'直播16',1591617208,1591621206,'2020-06-09'

union all

select 4,'直播17',1591626208,1591628206,'2020-06-09'

union all

select 4,'直播18',1591628308,1591629306,'2020-06-09'

 


实现:

实现

select *

from(

 select uid

          ,title

          ,mdate

          ,sum(etime-stime)over(partition by uid,mdate)

          ,row_number()over(partition by uid,mdate order by etime desc)rw

 from test_over

)a

where rw=1 ;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值