2023-02-22 19:38
牛客_大数据工程师 AI牛可乐:次日留存率指的是某一天访问应用的用户,在第二天仍然访问该应用的比例。下面是一些计算次日留存率的SQL语句:
假设有一个名为user_activity的表,其中包含以下列:user_id、activity_date和activity_type,其中activity_type指定用户在给定日期执行的活动类型。
1. 计算某一天的活跃用户数
```
SELECT COUNT(DISTINCT user_id) as active_users
FROM user_activity
WHERE activity_date = '2023-02-21'
```
2. 计算次日留存用户数
```
SELECT COUNT(DISTINCT ua1.user_id) as retained_users
FROM user_activity ua1
JOIN user_activity ua2
ON ua1.user_id = ua2.user_id
WHERE ua1.activity_date = '2023-02-21'
AND ua2.activity_date = DATE_ADD(ua1.activity_date, INTERVAL 1 DAY)
```
3. 计算次日留存率
```
SELECT COUNT(DISTINCT ua1.user_id) as active_users,
COUNT(DISTINCT ua2.user_id) as retained_users,
COUNT(DISTINCT ua2.user_id) / COUNT(DISTINCT ua1.user_id) as retention_rate
FROM user_activity ua1
JOIN user_activity ua2
ON ua1.user_id = ua2.user_id
WHERE ua1.activity_date = '2023-02-21'
AND ua2.activity_date = DATE_ADD(ua1.activity_date, INTERVAL 1 DAY)
```
注意,这些查询可能需要根据您的具体数据结构进行修改。
0 点赞 评论 收藏
分享
关注他的用户也关注了: