SQL查詢(xún)語(yǔ)句求出用戶(hù)的連續(xù)登陸天數(shù)
一、題目描述
求解用戶(hù)登陸信息表中,每個(gè)用戶(hù)連續(xù)登陸平臺(tái)的天數(shù),連續(xù)登陸基礎(chǔ)為匯總?cè)掌诒仨毜顷懀碇忻刻熘挥幸粭l用戶(hù)登陸數(shù)據(jù)(計(jì)算中不涉及天內(nèi)去重)。
表描述:user_id:用戶(hù)的id;
sigin_date:用戶(hù)的登陸日期。
二、解法分析
注:求解過(guò)程有多種方式,下述求解解法為筆者思路,其他解法可在評(píng)論區(qū)交流。
思路:
該問(wèn)題的突破的在于登陸時(shí)間,計(jì)算得到連續(xù)登陸標(biāo)識(shí),以標(biāo)識(shí)分組為過(guò)濾條件,得到連續(xù)登陸的天數(shù),最后以u(píng)ser_id分組,以count()函數(shù)求和得到每個(gè)用戶(hù)的連續(xù)登陸天數(shù)。
連續(xù)登陸標(biāo)識(shí) =(當(dāng)日登陸日期 - 用戶(hù)的登陸日期)- 開(kāi)窗排序的順序號(hào)(倒序)
三、求解過(guò)程及結(jié)果展示
1.數(shù)據(jù)準(zhǔn)備
-- 1.建表語(yǔ)句 drop table if exists test_sigindate_cnt; create table test_sigindate_cnt( user_id string ,sigin_date string ) ; -- 2.測(cè)試數(shù)據(jù)插入語(yǔ)句 insert overwrite table test_sigindate_cnt select 'uid_1' as user_id,'2021-08-03' as sigin_date union all select 'uid_1' as user_id,'2021-08-04' as sigin_date union all select 'uid_1' as user_id,'2021-08-01' as sigin_date union all select 'uid_1' as user_id,'2021-08-02' as sigin_date union all select 'uid_1' as user_id,'2021-08-05' as sigin_date union all select 'uid_1' as user_id,'2021-08-06' as sigin_date union all select 'uid_2' as user_id,'2021-08-01' as sigin_date union all select 'uid_2' as user_id,'2021-08-05' as sigin_date union all select 'uid_2' as user_id,'2021-08-02' as sigin_date union all select 'uid_2' as user_id,'2021-08-06' as sigin_date union all select 'uid_3' as user_id,'2021-08-04' as sigin_date union all select 'uid_3' as user_id,'2021-08-06' as sigin_date union all select 'uid_4' as user_id,'2021-08-03' as sigin_date union all select 'uid_4' as user_id,'2021-08-02' as sigin_date ;
2.計(jì)算過(guò)程
select user_id ,count(1) as sigin_cnt from ( select user_id ,datediff('2021-08-06',sigin_date) as data_diff ,row_number() over (partition by user_id order by sigin_date desc) as row_num from test_sigindate_cnt ) t where data_diff - row_num = -1 group by user_id ;
3.計(jì)算結(jié)果及預(yù)期結(jié)果對(duì)比
3.1 預(yù)期結(jié)果
匯總?cè)掌?/td> | 用戶(hù)id | 登陸天數(shù) |
2021-08-06 | uid_1 | 6 |
2021-08-06 | uid_2 | 2 |
2021-08-06 | uid_3 | 1 |
3.2 計(jì)算結(jié)果
以上就是SQL查詢(xún)語(yǔ)句求出用戶(hù)的連續(xù)登陸天數(shù)的詳細(xì)內(nèi)容,更多關(guān)于SQL語(yǔ)句求用戶(hù)的連續(xù)登陸天數(shù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SQL Server常見(jiàn)問(wèn)題及解決方法分享
這篇文章主要為大家詳細(xì)介紹了SQL Server常見(jiàn)問(wèn)題及解決方法,包括SQL Server連接問(wèn)題,SQL Server日志問(wèn)題,SQL Server查詢(xún)很久等問(wèn)題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01sql server編寫(xiě)archive通用模板腳本實(shí)現(xiàn)自動(dòng)分批刪除數(shù)據(jù)
這篇文章主要介紹了sql server編寫(xiě)archive通用模板腳本實(shí)現(xiàn)自動(dòng)分批刪除數(shù)據(jù),需要的朋友可以參考下2019-10-10sqlserver數(shù)據(jù)庫(kù)中的表、字段sql語(yǔ)句
在數(shù)據(jù)庫(kù)中創(chuàng)建的每個(gè)對(duì)象(例如約束、默認(rèn)值、日志、規(guī)則以及存儲(chǔ)過(guò)程)都對(duì)應(yīng)一行。2010-06-06數(shù)據(jù)庫(kù)日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(1)
下面小編就為大家?guī)?lái)一篇數(shù)據(jù)庫(kù)基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你2021-07-07SQLServer2019 數(shù)據(jù)庫(kù)環(huán)境搭建與使用的實(shí)現(xiàn)
這篇文章主要介紹了SQLServer2019 數(shù)據(jù)庫(kù)環(huán)境搭建與使用的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04詳解SQL Server數(shù)據(jù)庫(kù)鏈接查詢(xún)的方式
本文我們主要介紹了SQL Server數(shù)據(jù)庫(kù)鏈接查詢(xún)的方式,包括內(nèi)連接、外連接和交叉連接等的內(nèi)容,需要的朋友可以參考下2015-08-08sqlserver四舍五入使用round函數(shù)及cast和convert函數(shù)
大家在遇到sqlserver四舍五入除了用round函數(shù)還有沒(méi)有其他方法呢?下面小編給大家介紹使用cast和convert函數(shù),感興趣的朋友一起學(xué)習(xí)吧2015-11-11一個(gè)簡(jiǎn)單的SQL 行列轉(zhuǎn)換語(yǔ)句
在數(shù)據(jù)庫(kù)開(kāi)發(fā)中經(jīng)常會(huì)遇到行列轉(zhuǎn)換的問(wèn)題,比如下面的問(wèn)題,部門(mén),員工和員工類(lèi)型三張表,我們要統(tǒng)計(jì)類(lèi)似這樣的列表2009-08-08