Hive-SQL查詢連續(xù)活躍登錄用戶思路詳解
連續(xù)活躍登陸的用戶指至少連續(xù)2天都活躍登錄的用戶
解決類似場景的問題
創(chuàng)建數(shù)據(jù)
CREATE TABLE test5active(
dt string,
user_id string,
age int)
ROW format delimited fields terminated BY ',';
INSERT INTO TABLE test5active VALUES
('2019-02-11','user_1',23),('2019-02-11','user_2',19),
('2019-02-11','user_3',39),('2019-02-11','user_1',23),
('2019-02-11','user_3',39),('2019-02-11','user_1',23),
('2019-02-12','user_2',19),('2019-02-13','user_1',23),
('2019-02-15','user_2',19),('2019-02-16','user_2',19);
思路一:
1、因為每天用戶登錄次數(shù)可能不止一次,所以需要先將用戶每天的登錄日期去重。
2、再用row_number() over(partition by _ order by _)函數(shù)將用戶id分組,按照登陸時間進(jìn)行排序。
3、計算登錄日期減去第二步驟得到的結(jié)果值,用戶連續(xù)登陸情況下,每次相減的結(jié)果都相同。
4、按照id和日期分組并求和,篩選大于等于2的即為連續(xù)活躍登陸的用戶。
第一步:用戶登錄日期去重
select DISTINCT dt,user_id from test5active;

第二步:用row_number() over()函數(shù)計數(shù)
select t1.user_id,t1.dt, row_number() over(partition by t1.user_id order by t1.dt) day_rank from ( select DISTINCT dt,user_id from test5active )t1;

第三步:日期減去計數(shù)值得到結(jié)果
select t2.user_id,t2.dt,date_sub(t2.dt,t2.day_rank) as dis from ( select t1.user_id,t1.dt, row_number() over(partition by t1.user_id order by t1.dt) day_rank from ( select DISTINCT dt,user_id from test5active )t1)t2;

第四步:根據(jù)id和結(jié)果分組并計算總和,大于等于2的即為連續(xù)登陸的用戶,得到 用戶id,開始日期,結(jié)束日期,連續(xù)登錄天數(shù)
select t3.user_id,min(t3.dt),max(t3.dt),count(1) from ( select t2.user_id,t2.dt,date_sub(t2.dt,t2.day_rank) as dis from ( select t1.user_id,t1.dt, row_number() over(partition by t1.user_id order by t1.dt) day_rank from ( select DISTINCT dt,user_id from test5active )t1 )t2 )t3 group by t3.user_id,t3.dis having count(1)>1;
用戶id 開始日期 結(jié)束日期 連續(xù)登錄天數(shù)

最后:連續(xù)登陸的用戶
select distinct t4.user_id from ( select t3.user_id,min(t3.dt),max(t3.dt),count(1) from ( select t2.user_id,t2.dt,date_sub(t2.dt,t2.day_rank) as dis from ( select t1.user_id,t1.dt, row_number() over(partition by t1.user_id order by t1.dt) day_rank from ( select DISTINCT dt,user_id from test5active )t1 )t2 )t3 group by t3.user_id,t3.dis having count(1)>1 )t4;

思路二:使用lag(向后)或者lead(向前)
select user_id,t1.dt, lead(t1.dt) over(partition by user_id order by t1.dt) as last_date_id from ( select DISTINCT dt,user_id from test5active )t1;

select distinct t2.user_id from ( select user_id,t1.dt, lead(t1.dt) over(partition by user_id order by t1.dt) as last_date_id from ( select DISTINCT dt,user_id from test5active )t1 )t2 where datediff(last_date_id,t2.dt)=1;

參考:
到此這篇關(guān)于Hive-SQL查詢連續(xù)活躍登陸的用戶的文章就介紹到這了,更多相關(guān)SQL查詢連續(xù)登陸的用戶內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SQL Server中使用sp_password重置SA密碼實例
這篇文章主要介紹了SQL Server中使用sp_password重置SA密碼實例,一般在忘記SA密碼時的恢復(fù)手段,需要的朋友可以參考下2014-08-08
windows11安裝sqlserver?2016數(shù)據(jù)庫報錯等待數(shù)據(jù)庫引擎恢復(fù)句柄失敗解決辦法
最近安裝SQL?Server遇到這個問題,試過網(wǎng)上幾乎所有辦法,都安裝不上,查了很久才解決,下面這篇文章主要給大家介紹了關(guān)于windows11安裝SQL?server數(shù)據(jù)庫報錯等待數(shù)據(jù)庫引擎恢復(fù)句柄失敗的解決辦法,需要的朋友可以參考下2023-06-06
SQL Server2022安裝教程的實現(xiàn)步驟(圖文教程)
在日常的工作中,sql server作為一款常用的數(shù)據(jù)庫管理系統(tǒng),安裝與配置就顯得非常重要,本文主要介紹了SQL Server2022安裝教程的實現(xiàn)步驟,感興趣的可以了解一下2023-09-09
sql注入數(shù)據(jù)庫修復(fù)的兩種實例方法
這篇文章介紹了sql注入數(shù)據(jù)庫修復(fù)的兩種實例方法,有需要的朋友可以參考一下2013-09-09
SQL?Server中T-SQL標(biāo)識符介紹與無排序生成序號的方法
這篇文章介紹了SQL?Server中T-SQL標(biāo)識符與無排序生成序號的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
sqlserver游標(biāo)使用步驟示例(創(chuàng)建游標(biāo) 關(guān)閉游標(biāo))
這篇文章主要介紹了sqlserver游標(biāo)使用步驟,包括創(chuàng)建游標(biāo)、關(guān)閉游標(biāo),大家參考使用吧2014-01-01

