gpt4 book ai didi

mysql - MySQL 的保留率

转载 作者:行者123 更新时间:2023-11-30 21:58:00 24 4
gpt4 key购买 nike

我尝试用 mysql 计算保留率并从这个开始:

SELECT 
s_order.ordertime,
DATE_SUB(future_orders.ordertime, INTERVAL 90 DAY),
count(distinct s_order.userID) as active_users,
count(distinct future_orders.userID) as retained_users
FROM s_order
LEFT JOIN s_order as future_orders on
s_order.userID = future_orders.userID
AND s_order.ordertime = DATE_SUB(future_orders.ordertime, INTERVAL 90 DAY);

这不起作用 - 我让所有用户都处于事件状态,因此我将 DATE_SUB(future_orders.ordertime, INTERVAL 90 DAY), 添加到选择标准以查看发生了什么。然而它返回 NULL - 但为什么呢?

作为引用,我确实看过这个解释: https://www.periscopedata.com/blog/how-to-calculate-cohort-retention-in-sql.html

我的表有这样的结构

s_orders:
ID | userID | ordertime

我希望得到一个结果,即在过去 90 天内有多少不同的用户总体上订购了某样东西,有多少人再次订购了某样东西,以留住客户。

有人知道我在 MySQL 中做错了什么吗?

最佳答案

DATE_SUB() 在日期值为 null 时返回 null,这可能是原因。因为您是 LEFT JOIN-ing,所以 future_orders 记录可以为空/不存在

关于mysql - MySQL 的保留率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44407543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com