Hive-SQL查詢連續(xù)活躍登錄用戶思路詳解
連續(xù)活躍登陸的用戶指至少連續(xù)2天都活躍登錄的用戶
解決類似場景的問題
創(chuàng)建數據
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、因為每天用戶登錄次數可能不止一次,所以需要先將用戶每天的登錄日期去重。
2、再用row_number() over(partition by _ order by _)函數將用戶id分組,按照登陸時間進行排序。
3、計算登錄日期減去第二步驟得到的結果值,用戶連續(xù)登陸情況下,每次相減的結果都相同。
4、按照id和日期分組并求和,篩選大于等于2的即為連續(xù)活躍登陸的用戶。
第一步:用戶登錄日期去重
select DISTINCT dt,user_id from test5active;
第二步:用row_number() over()函數計數
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;
第三步:日期減去計數值得到結果
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;
第四步:根據id和結果分組并計算總和,大于等于2的即為連續(xù)登陸的用戶,得到 用戶id,開始日期,結束日期,連續(xù)登錄天數
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 開始日期 結束日期 連續(xù)登錄天數
最后:連續(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;
參考:
到此這篇關于Hive-SQL查詢連續(xù)活躍登陸的用戶的文章就介紹到這了,更多相關SQL查詢連續(xù)登陸的用戶內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SQL Server中使用sp_password重置SA密碼實例
這篇文章主要介紹了SQL Server中使用sp_password重置SA密碼實例,一般在忘記SA密碼時的恢復手段,需要的朋友可以參考下2014-08-08windows11安裝sqlserver?2016數據庫報錯等待數據庫引擎恢復句柄失敗解決辦法
最近安裝SQL?Server遇到這個問題,試過網上幾乎所有辦法,都安裝不上,查了很久才解決,下面這篇文章主要給大家介紹了關于windows11安裝SQL?server數據庫報錯等待數據庫引擎恢復句柄失敗的解決辦法,需要的朋友可以參考下2023-06-06SQL Server2022安裝教程的實現(xiàn)步驟(圖文教程)
在日常的工作中,sql server作為一款常用的數據庫管理系統(tǒng),安裝與配置就顯得非常重要,本文主要介紹了SQL Server2022安裝教程的實現(xiàn)步驟,感興趣的可以了解一下2023-09-09SQL?Server中T-SQL標識符介紹與無排序生成序號的方法
這篇文章介紹了SQL?Server中T-SQL標識符與無排序生成序號的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05sqlserver游標使用步驟示例(創(chuàng)建游標 關閉游標)
這篇文章主要介紹了sqlserver游標使用步驟,包括創(chuàng)建游標、關閉游標,大家參考使用吧2014-01-01