雪花客户保留率
Customer retention rate in snowflake
我有这个客户保留率公式,但有点难以将其转换为雪花中的 sql 代码:
客户保留率:过去和[过去 30 天]期间购买的客户数量/过去[不包括过去 30 天]购买的客户数量
如果我们检查每个客户是否有“旧销售”和“新销售”
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_ID
MIN_DATE
MAX_DATE
OLD_SALES
NEW_SALES
1
2022-05-01
2022-05-01
FALSE
TRUE
2
2022-04-01
2022-05-01
TRUE
TRUE
3
2022-04-01
2022-04-01
TRUE
FALSE
4
2022-03-01
2022-04-01
TRUE
FALSE
然后我们想把顶部都算为真,把底部都算为 trop/false,我们可以使用:
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
);
并得到:
TOP
BOTTOM
RENTION
1
2
0.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%
我有这个客户保留率公式,但有点难以将其转换为雪花中的 sql 代码:
客户保留率:过去和[过去 30 天]期间购买的客户数量/过去[不包括过去 30 天]购买的客户数量
如果我们检查每个客户是否有“旧销售”和“新销售”
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_ID | MIN_DATE | MAX_DATE | OLD_SALES | NEW_SALES |
---|---|---|---|---|
1 | 2022-05-01 | 2022-05-01 | FALSE | TRUE |
2 | 2022-04-01 | 2022-05-01 | TRUE | TRUE |
3 | 2022-04-01 | 2022-04-01 | TRUE | FALSE |
4 | 2022-03-01 | 2022-04-01 | TRUE | FALSE |
然后我们想把顶部都算为真,把底部都算为 trop/false,我们可以使用:
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
);
并得到:
TOP | BOTTOM | RENTION |
---|---|---|
1 | 2 | 0.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%