亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SQL計(jì)算用戶(hù)留存率問(wèn)題

 更新時(shí)間:2024年09月02日 11:23:49   作者:黑白交界  
電商業(yè)務(wù)中需要計(jì)算用戶(hù)留存情況,在時(shí)間維度上可以分為用戶(hù)次日、多日后的留存情況,本文就來(lái)詳細(xì)的介紹一下如何計(jì)算,具有一定的參考價(jià)值,感興趣的可以了解一下

概念

電商業(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)文章

最新評(píng)論