日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

两表左连接count某一字段_表连接解决多日留存率问题|SQL

發(fā)布時間:2023/12/15 数据库 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 两表左连接count某一字段_表连接解决多日留存率问题|SQL 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、留存率的計算

留存率=新增用戶中登錄用戶數(shù)/新增用戶數(shù)*100%

第N日留存:指的是新增用戶日之后的第N日依然登錄的用戶數(shù)/新增用戶數(shù)*100%

二、數(shù)據(jù)集的理解

表【登錄情況】中有字段【用戶id】,【登陸日期】,用戶id和登陸日期能夠標識一行數(shù)據(jù)。

登陸情況表的部分數(shù)據(jù)

三、計算多日留存率

(1)將日期、用戶id做關(guān)聯(lián),進行多個連接。

注意:

1、新增用戶的定義:某日的新增用戶是指在這一天之前沒有登陸信息的用戶。

每一天產(chǎn)生的新增用戶名單作表a,思路如下:
將登陸情況和登陸情況連接——表s1和s2
以用戶id和日期做關(guān)聯(lián)條件——s2 on s1.用戶id=s2.用戶id and s1.日期>s2.日期
再加上查詢條件——s2.日期 is null

2、只計算次日留存率需要連接2個表,計算次日、二日、三日、四日留存率需要連接四個表。

計算次日留存率思路如下:
將a表和登陸情況連接——表a和表b
以用戶id和日期作關(guān)聯(lián)條件——a.用戶id=b.用戶id and b.日期=date_add(a.日期,interval 1 day)
再將b表中的留存用戶數(shù)取出 ——count(distinct b.用戶id)
計算留存率——concat、round

select a.日期,count(distinct a.用戶id) as 日新增用戶數(shù),concat(round(100*count(distinct b.用戶id)/count(distinct a.用戶id),2),'%') as 次日留存率,concat(round(100*count(distinct c.用戶id)/count(distinct a.用戶id),2),'%') as 二日留存率,concat(round(100*count(distinct d.用戶id)/count(distinct a.用戶id),2),'%') as 三日留存率,concat(round(100*count(distinct e.用戶id)/count(distinct a.用戶id),2),'%') as 四日留存率from(select s1.*from 登陸情況 as s1 left join 登陸情況 as s2 on s1.用戶id=s2.用戶id and s1.日期>s2.日期where s2.日期 is null)as aleft join 登陸情況 as b on a.用戶id=b.用戶id and b.日期=date_add(a.日期,interval 1 day)left join 登陸情況 as c on a.用戶id=c.用戶id and c.日期=date_add(a.日期,interval 2 day)left join 登陸情況 as d on a.用戶id=d.用戶id and d.日期=date_add(a.日期,interval 3 day)left join 登陸情況 as e on a.用戶id=e.用戶id and e.日期=date_add(a.日期,interval 4 day)group by a.日期

上面自連接的方法固然可行但多次關(guān)聯(lián)在執(zhí)行效率上會有瓶頸。更好的方法如下:

(2)將用戶id做關(guān)聯(lián),進行一個連接,直接在最外層查詢時根據(jù)自己的目標限定日期差。

注意:

1、a表和上面的相同

2、只連接a表和登陸情況表,只以用戶id為關(guān)聯(lián)條件

3、外層查詢用到 if 和 datediff 來限定日期差,如果滿足條件,則返回b表的用戶id。

select a.日期, count(distinct a.用戶id) as 日新增用戶數(shù), concat(round(100*count(distinct if(datediff(b.日期,a.日期)=1, b.用戶id, null))/count(distinct a.用戶id),2),'%') as 次日留存率, concat(round(100*count(distinct if(datediff(b.日期,a.日期)=2, b.用戶id, null))/count(distinct a.用戶id),2),'%') as 二日留存率, concat(round(100*count(distinct if(datediff(b.日期,a.日期)=3, b.用戶id, null))/count(distinct a.用戶id),2),'%') as 三日留存率, concat(round(100*count(distinct if(datediff(b.日期,a.日期)=4, b.用戶id, null))/count(distinct a.用戶id),2),'%') as 四日留存率, concat(round(100*count(distinct if(datediff(b.日期,a.日期)=5, b.用戶id, null))/count(distinct a.用戶id),2),'%') as 五日留存率, concat(round(100*count(distinct if(datediff(b.日期,a.日期)=6, b.用戶id, null))/count(distinct a.用戶id),2),'%') as 六日留存率, concat(round(100*count(distinct if(datediff(b.日期,a.日期)=7, b.用戶id, null))/count(distinct a.用戶id),2),'%') as 七日留存率, concat(round(100*count(distinct if(datediff(b.日期,a.日期)=8, b.用戶id, null))/count(distinct a.用戶id),2),'%') as 八日留存率 from (select s1.*from 登陸情況 as s1 left join 登陸情況 as s2 on s1.用戶id=s2.用戶id and s1.日期>s2.日期where s2.日期 is null) as a #每日新增的用戶名單 left join 登陸情況 as b on a.用戶id=b.用戶id group by a.日期

總結(jié)

以上是生活随笔為你收集整理的两表左连接count某一字段_表连接解决多日留存率问题|SQL的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。