雪花的客户保留率

发布于 2025-01-31 03:27:46 字数 47 浏览 4 评论 0原文

我有这个用于客户保留率的

公式过去购买的客户[不包括最近30天]

I have this formula for the customer retention rate but kinda stuck in translating it into a sql code in snowflake:

Customer Retention rate: number of customers who purchased in the past AND in the period of [last 30 days] / number of customers who have purchased in the past [not including last 30 days]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凝望流年 2025-02-07 03:27:46

如果我们检查每个客户是否有“旧销售”和“新销售

select customer_id, min(date) as min_date, max(date) as max_date, min_date < (current_date()-30) as old_sales, max_date >= (current_date()-30) as new_sales
from values
    (1,'2022-05-01'),
    (2,'2022-05-01'),
    (2,'2022-04-01'),
    (3,'2022-04-01'),
    (4,'2022-04-01'),
    (4,'2022-03-01')
    t(customer_id, date)
group by 1

得到

customer_idmin_datemax_dateold_salesnew_sales
12022-05-05-012022-05-05
我们真实true
32022-04-012022-04-01truefalse
42022-03-012022-04-01false

我们想将两者都视为顶部的true,而trop/false to the the the the,我们可以使用:

select count_if(old_sales and new_sales) as top
    ,count_if(old_sales and  new_sales=false) as bottom
    ,div0(top, bottom) as rention
from (
    select customer_id, min(date) as min_date, max(date) as max_date, min_date < (current_date()-30) as old_sales, max_date >= (current_date()-30) as new_sales
    from values
        (1,'2022-05-01'),
        (2,'2022-05-01'),
        (2,'2022-04-01'),
        (3,'2022-04-01'),
        (4,'2022-04-01'),
        (4,'2022-03-01')
        t(customer_id, date)
    group by 1
);

并获取:

最底部典范
120.5

因此,使用data_table作为我们可以使用的来源:

select div0(count_if(old_sales and new_sales), count_if(old_sales and  new_sales=false)) as rention
from (
    select 
        min(date) < (current_date()-30) as old_sales, 
        max(date) >= (current_date()-30) as new_sales
    from data_table
    group by customer_id
);

获得魔术50%

if we check for each customer if they have "old sales" and "new sales"

select customer_id, min(date) as min_date, max(date) as max_date, min_date < (current_date()-30) as old_sales, max_date >= (current_date()-30) as new_sales
from values
    (1,'2022-05-01'),
    (2,'2022-05-01'),
    (2,'2022-04-01'),
    (3,'2022-04-01'),
    (4,'2022-04-01'),
    (4,'2022-03-01')
    t(customer_id, date)
group by 1

we get:

CUSTOMER_IDMIN_DATEMAX_DATEOLD_SALESNEW_SALES
12022-05-012022-05-01FALSETRUE
22022-04-012022-05-01TRUETRUE
32022-04-012022-04-01TRUEFALSE
42022-03-012022-04-01TRUEFALSE

we then want to count both as true for the top, and trop/false for the bottom, we can use:

select count_if(old_sales and new_sales) as top
    ,count_if(old_sales and  new_sales=false) as bottom
    ,div0(top, bottom) as rention
from (
    select customer_id, min(date) as min_date, max(date) as max_date, min_date < (current_date()-30) as old_sales, max_date >= (current_date()-30) as new_sales
    from values
        (1,'2022-05-01'),
        (2,'2022-05-01'),
        (2,'2022-04-01'),
        (3,'2022-04-01'),
        (4,'2022-04-01'),
        (4,'2022-03-01')
        t(customer_id, date)
    group by 1
);

and get:

TOPBOTTOMRENTION
120.5

thus with a data_table as our source we can use:

select div0(count_if(old_sales and new_sales), count_if(old_sales and  new_sales=false)) as rention
from (
    select 
        min(date) < (current_date()-30) as old_sales, 
        max(date) >= (current_date()-30) as new_sales
    from data_table
    group by customer_id
);

to get the magic 50%

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文