Hive SQL实现每小时登录时长占比指标计算

需求描述

用Hive SQL实现每小时登录时长指标,登陆时间和登出时间跨整小时也需要计算

登录时间登出时间登陆时长(s)
2022-04-04 08:00:102022-04-04 11:20:2012010

如上面表格所示,原始数据需转换为下面表格情况,每个整小时也需要计算

登陆时间段登陆时间登出时间
082022-04-04 08:00:102022-04-04 09:00:00
092022-04-04 09:00:002022-04-04 10:00:00
102022-04-04 10:00:102022-04-04 11:00:00
112022-04-04 11:00:102022-04-04 11:20:20

数据准备

--建表
CREATE TABLE `test`(`id` string, `checkin` timestamp, `checkout` timestamp)

--插入数据
insert into table test select '1',cast('2022-04-04 08:00:10' as timestamp),cast('2022-04-04 11:20:20' as timestamp);
insert into table test select '1',cast('2022-04-04 02:00:03' as timestamp),cast('2022-04-04 03:40:30' as timestamp);
insert into table test select '1',cast('2022-04-04 04:17:11' as timestamp),cast('2022-04-04 07:44:46' as timestamp);
insert into table test select '1',cast('2022-04-04 07:50:11' as timestamp),cast('2022-04-04 07:59:59' as timestamp);

idcheckincheckout
12022-04-04 08:00:102022-04-04 11:20:20
12022-04-04 04:17:112022-04-04 07:44:46
12022-04-04 07:50:112022-04-04 07:59:59
12022-04-04 02:00:032022-04-04 03:40:30

逻辑实现

select 
id,
to_date(checkin2) check_date,
hour(checkin2) check_hour,
checkin2 checkin,
if(unix_timestamp(checkout2)>unix_timestamp(checkout),checkout,checkout2) checkout
from (
select
    id,
    sign,
    checkout,
    case when sign<>0 then concat(date_format(checkin,'YYYY-MM-dd'),' ',
        if(length((hour(checkin)+sign))=1,concat('0',cast((hour(checkin)+sign) as string)),cast((hour(checkin)+sign) as string))
        ,':00:00') else checkin end checkin2,
    case when hour(checkin)<>hour(checkout) then concat(date_format(checkin,'YYYY-MM-dd'),' ',
        if(length((hour(checkin)+sign+1))=1,concat('0',cast((hour(checkin)+sign+1) as string)),cast((hour(checkin)+sign+1) as string))
        ,':00:00' )
    else checkout end checkout2
from(
        select * from lx_time
    ) tmp lateral view posexplode(
        split(space(hour(checkout) - hour(checkin) + (datediff(checkout,checkin)) * 24),'')
    ) t as sign,sign2
) f ;
idcheck_datecheck_hourcheckincheckout
12022-04-0442022-04-04 04:17:112022-04-04 05:00:00
12022-04-0452022-04-04 05:00:002022-04-04 06:00:00
12022-04-0462022-04-04 06:00:002022-04-04 07:00:00
12022-04-0472022-04-04 07:00:002022-04-04 07:44:46
12022-04-0472022-04-04 07:50:112022-04-04 07:59:59
12022-04-0422022-04-04 02:00:032022-04-04 03:00:00
12022-04-0432022-04-04 03:00:002022-04-04 03:40:30
12022-04-0482022-04-04 08:10:202022-04-04 08:20:20

至此主体逻辑已经完成,接下来就是根据主键,登录日期,登陆时间段分组 sum(登出时间-登陆时间)即可

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值