Hive-SQL查詢連續(xù)活躍登錄用戶思路詳解
連續(xù)活躍登陸的用戶指至少連續(xù)2天都活躍登錄的用戶
解決類似場(chǎng)景的問題
創(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、因?yàn)槊刻煊脩舻卿洿螖?shù)可能不止一次,所以需要先將用戶每天的登錄日期去重。
2、再用row_number() over(partition by _ order by _)函數(shù)將用戶id分組,按照登陸時(shí)間進(jìn)行排序。
3、計(jì)算登錄日期減去第二步驟得到的結(jié)果值,用戶連續(xù)登陸情況下,每次相減的結(jié)果都相同。
4、按照id和日期分組并求和,篩選大于等于2的即為連續(xù)活躍登陸的用戶。
第一步:用戶登錄日期去重
select DISTINCT dt,user_id from test5active;

第二步:用row_number() over()函數(shù)計(jì)數(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;

第三步:日期減去計(jì)數(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é)果分組并計(jì)算總和,大于等于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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SQL Server 數(shù)據(jù)文件收縮和查看收縮進(jìn)度的步驟
這篇文章主要介紹了SQL Server 數(shù)據(jù)文件收縮和查看收縮進(jìn)度,SQL Server在刪除數(shù)據(jù)后,會(huì)重新利用這部分空間,所以如果不是空間緊張的情況下,可以不回收,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
SQL中的開窗函數(shù)詳解可代替聚合函數(shù)使用
這篇文章主要介紹了SQL中的開窗函數(shù)詳解可代替聚合函數(shù)使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Sqlview動(dòng)態(tài)發(fā)布地圖圖層的方法
這篇文章主要介紹了Sqlview動(dòng)態(tài)發(fā)布地圖圖層的方法的相關(guān)資料,需要的朋友可以參考下2016-03-03
VS連接SQL?server數(shù)據(jù)庫及實(shí)現(xiàn)基本CRUD操作
這篇文章主要給大家介紹了關(guān)于VS連接SQL?server數(shù)據(jù)庫及實(shí)現(xiàn)基本CRUD操作的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-01-01
sql時(shí)間段切分實(shí)現(xiàn)每隔x分鐘出一份高速門架車流量
本文主要介紹了sql時(shí)間段切分實(shí)現(xiàn)每隔x分鐘出一份高速門架車流量,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
同一個(gè)sql語句 連接兩個(gè)數(shù)據(jù)庫服務(wù)器
在sqlserver查詢分析器中,一個(gè)sql語句連接兩個(gè)數(shù)據(jù)庫服務(wù)器的一種方法2009-08-08
SQL Server 數(shù)據(jù)庫索引其索引的小技巧
關(guān)于索引的常識(shí):影響到數(shù)據(jù)庫性能的最大因素就是索引。由于該問題的復(fù)雜性,我只可能簡單的談?wù)勥@個(gè)問題,不過關(guān)于這方面的問題,目前有好幾本不錯(cuò)的書籍可供你參閱。我在這里只討論兩種SQL Server索引,即clustered索引和nonclustered索引2012-06-06

