SQL--统计出每日在线总数

需求:

一张表有uid、起始日期和终止日期,能不能一条sql统计出每日在线总数

样例:

比如 三条数据:

a 20220510 20220512

b 20220510 20220511

c 20220511 20220512

结果:

20220510 2

20220511 3

20220512 2

数据准备:

CREATE TABLE brand as
    select 'a' as uid, '2022-05-10' as start_date, '2022-05-12' as end_date union all
    select 'b' as uid, '2022-05-10' as start_date, '2022-05-11' as end_date  union all
    select 'c' as uid, '2022-05-11' as start_date, '2022-05-12' as end_date
    ;

参考答案:

将时间打散 --对时间区间打散到每一天,在按照日期去重count

select
tt,count(distinct uid)
from
(
select
uid
,start_date,end_date
,indx,val
,date_add(start_date,indx) tt
from brand lateral view posexplode(split(space(datediff(end_date,start_date)),'')) t as indx,val
)tmp
group by tt
;

知识点

1.space()函数

hive>select split(space(10), '');
[" "," "," "," "," "," "," "," "," "," ",""]

2.posexplode()函数

select  i, date_add('2020-11-01', pe.i) as dynamic_date ,'2020-11-01' as start_time, '2020-11-30' end_time
from  ods.test  
lateral view posexplode(split(space(datediff('2020-11-30', '2020-11-01')),' ')) pe as i, x  limit 30 ;

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值