hive sql之:最大登录天数,获取连续登录指定天数

这篇博客探讨了如何使用SQL进行数据查询,包括创建表`test2`并插入数据,然后通过子查询和窗口函数计算最大登录天数和连续登录指定天数的记录。还展示了`DATEDIFF`函数的用法,筛选日期间隔大于特定值的记录。
摘要由CSDN通过智能技术生成
create table test2(
  id string,
  pday string
);

INSERT INTO test2(id,pday) values ('A','20190701');
INSERT INTO test2(id,pday) values ('A','20190702');
INSERT INTO test2(id,pday) values ('A','20190703');
INSERT INTO test2(id,pday) values ('A','20190704');
INSERT INTO test2(id,pday) values ('A','20190706');
INSERT INTO test2(id,pday) values ('A','20190707');
INSERT INTO test2(id,pday) values ('A','20190708');
INSERT INTO test2(id,pday) values ('A','20190711');
INSERT INTO test2(id,pday) values ('A','20190712');

INSERT INTO test2(id,pday) values ('B','20190629');
INSERT INTO test2(id,pday) values ('B','20190630');
INSERT INTO test2(id,pday) values ('B','20190701');
INSERT INTO test2(id,pday) values ('B','20190704');
INSERT INTO test2(id,pday) values ('B','20190706');

最大登录天数

select 
  t2.id,
  max(t2.num)
from 
(
	select 
	t.id as id,
	count(t.sub) num
	from 
	(
		select 
			id,
			pday,
			date_sub(
				from_unixtime(unix_timestamp(pday,'yyyyMMdd'),'yyyy-MM-dd'),
				row_number() over(partition by id order by pday)
			) as sub
		from test2
	) as t
	group by t.id,t.sub
) t2
group by t2.id;

获取连续登录指定天数的:

select
t.id as id,
t.pday as pday,
date_sub(t.pday,rn) as data_sub,
t.rn as rn
from 
(
	select 
	id,
	from_unixtime(unix_timestamp(pday,'yyyyMMdd'),'yyyy-MM-dd') as pday,
	row_number() over(partition by id order by pday desc) as rn
	from test2
) t
where t.rn = 3;

===============================================
datediff的用法

select *
from 
(
select 
id,
from_unixtime(unix_timestamp(pday,'yyyyMMdd'),'yyyy-MM-dd') as pday,
date_sub(
from_unixtime(unix_timestamp(pday,'yyyyMMdd'),'yyyy-MM-dd'),
row_number() over(partition by id order by pday)
) date_sub
from test2
) t2 
where datediff(t2.pday,t2.date_sub) > 2;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

涂作权的博客

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

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

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

打赏作者

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

抵扣说明:

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

余额充值