SQL計(jì)算用戶(hù)留存率問(wèn)題
概念
電商業(yè)務(wù)中需要計(jì)算用戶(hù)留存情況,在時(shí)間維度上可以分為用戶(hù)次日、多日后的留存情況,用留存率表示。留存率計(jì)算方式如下:
次日留存率 = 當(dāng)日新用戶(hù)在次日登錄的數(shù)量 / 當(dāng)日新用戶(hù)注冊(cè)數(shù)量
三日留存率 = 當(dāng)日新用戶(hù)在第四天登錄的數(shù)量 / 當(dāng)日新用戶(hù)注冊(cè)數(shù)量
七日留存率 = 當(dāng)日新用戶(hù)在第八天登錄的數(shù)量 / 當(dāng)日新用戶(hù)注冊(cè)數(shù)量
其他時(shí)間的留存率計(jì)算同理。
情況一
假設(shè)現(xiàn)在有兩張表,一張是注冊(cè)信息表register_info,另一張是登錄信息表login_info,兩個(gè)表格數(shù)據(jù)如下。
現(xiàn)在分別計(jì)算出每日的新增用戶(hù)數(shù),以及次日、三日、七日留存率。
首先,將兩張表做一個(gè)連接,用datediff函數(shù)選出用戶(hù)注冊(cè)后七日仍有登錄的數(shù)據(jù)(條件1),連接條件是用戶(hù)id相同(條件2)。查詢(xún)代碼如下:
select r.uid, date(r.register_date) as rt, date(l.login_time) as lt, datediff(l.login_time, r.register_date) as tdiff from register_info r left join login_info l on r.uid=l.uid and date(l.login_time) between date(r.register_date)+interval 1 day and date(r.register_date)+interval 7 day order by uid asc
查詢(xún)結(jié)果如下:(這里每個(gè)用戶(hù)的注冊(cè)時(shí)間只有一個(gè),所以會(huì)得出每個(gè)用戶(hù)所有注冊(cè)登錄時(shí)間的排列組合便于后續(xù)計(jì)算)
之后利用case條件判斷和count函數(shù)計(jì)算出各種留存率情況。代碼如下:
select rt as 日期, count(distinct uid) as 新增用戶(hù)數(shù), count(distinct case when tdiff=1 then uid end)/count(distinct uid) as 次日留存率, count(distinct case when tdiff=3 then uid end)/count(distinct uid) as 三日留存率, count(distinct case when tdiff=7 then uid end)/count(distinct uid) as 七日留存率 from ( select r.uid, date(r.register_date) as rt, date(l.login_time) as lt, datediff(l.login_time, r.register_date) as tdiff from register_info r left join login_info l on r.uid=l.uid and date(l.login_time) between date(r.register_date)+interval 1 day and date(r.register_date)+interval 7 day order by uid asc ) t1 group by rt
得到結(jié)果如下:
情況二
假如現(xiàn)在只有一張用戶(hù)登錄信息表user_login_info,其中默認(rèn)每個(gè)用戶(hù)最早的登錄時(shí)間就是注冊(cè)時(shí)間,表格數(shù)據(jù)如下:
所以我們需要先用兩個(gè)子查詢(xún)得出類(lèi)似(1)中的注冊(cè)表t1和登錄表t2,之后操作與(1)中相同,代碼如下:
select count(distinct uid) 新增用戶(hù)數(shù), count(distinct case when tdiff=1 then uid end)/count(distinct uid) as 次日留存率, count(distinct case when tdiff=3 then uid end)/count(distinct uid) as 三日留存率, count(distinct case when tdiff=7 then uid end)/count(distinct uid) as 七日留存率 from ( select t1.uid, t1.rt, t2.lt, datediff(t2.lt,t1.rt) as tdiff from ( (select user_id as uid, min(date(login_time)) as rt from user_login_info group by user_id) t1 LEFT JOIN (select user_id as uid, date(login_time) as lt from user_login_info) t2 on t1.uid=t2.uid and t2.lt between t1.rt + interval 1 day and t1.rt + interval 7 day ) ) t3 group by rt order by rt asc
其中t3就是情況一中得到的t1.
查詢(xún)結(jié)果如下:
到此這篇關(guān)于SQL計(jì)算用戶(hù)留存率問(wèn)題的文章就介紹到這了,更多相關(guān)SQL 用戶(hù)留存率內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ASP.NET下向SQLServer2008導(dǎo)入文件實(shí)例操作方法
在Microsoft SQL Server2008以后的版本中,將刪除image數(shù)據(jù)類(lèi)型。在新的開(kāi)發(fā)工作中將不適用此數(shù)據(jù)類(lèi)型,并打算修改當(dāng)前使用此數(shù)據(jù)類(lèi)型的應(yīng)用程序,改用varbinary(max)數(shù)據(jù)類(lèi)型。2010-09-09sqlserver獲取當(dāng)前日期的最大時(shí)間值
如果你有看到這篇,會(huì)看到Insus.NET在SQL Server2008使用最簡(jiǎn)單的方法取到午夜時(shí)間值。2011-09-09SQL Server 數(shù)據(jù)庫(kù)的設(shè)計(jì)詳解
這篇文章主要為大家介紹了SQLServer數(shù)據(jù)庫(kù)的設(shè)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-01-01sql腳本查詢(xún)數(shù)據(jù)庫(kù)表,數(shù)據(jù),結(jié)構(gòu),約束等操作的方法
本文介紹了“sql腳本查詢(xún)數(shù)據(jù)庫(kù)表,數(shù)據(jù),結(jié)構(gòu),約束等操作的方法”,需要的朋友可以參考一下2013-03-03SQL Server中選出指定范圍行的SQL語(yǔ)句代碼
SQL Server中選出指定范圍行的SQL語(yǔ)句代碼寫(xiě)法實(shí)例2008-07-07SQL?Server設(shè)置多個(gè)端口號(hào)的操作步驟
SQL?Server使用的默認(rèn)端口號(hào)是TCP端口1433,這是為了連接到?Microsoft?SQL?Server?實(shí)例的標(biāo)準(zhǔn)網(wǎng)絡(luò)端口,如果你正在設(shè)置?SQL?Server?或者嘗試從其他應(yīng)用程序連接到它,所以本文給大家介紹了SQL?Server如何設(shè)置多個(gè)端口號(hào),需要的朋友可以參考下2024-07-07