【问题标题】:how to produce a customer retention table /cohort analysis with SQL如何使用 SQL 生成客户保留表/队列分析
【发布时间】:2019-06-02 05:21:17
【问题描述】:

我正在尝试编写 SQL 查询(Presto SQL 语法)来生成客户保留表(参见下面的示例)。 一个月内至少完成一笔交易的客户将被视为当月留存。

这是桌子

user_id       transaction_date
bdcff651- .   2018-01-01
bdcff641 .     2018-03-15

这是我想要的结果

第一行应该这样理解: 在 2018 年 1 月(定义为“Jan Activation Cohort”)进行首次交易的所有客户中,35% 随后在第一次交易日期后的一个月内进行了交易,23% 在下个月进行了交易,15下个月的百分比等等。

  Date         1st Month   2nd Month       3rd Month

2018-01-01      35%               23% .         15%
2018-02-0       33 %             26% .         13%
2018-03-0       36%               27%           12%

例如,如果某人 XYZ 在 2018 年 2 月 10 日进行了他的第一笔交易,那么他的第一个月将是从 2018 年 2 月 11 日到 2018 年 3 月 10 日,第二个月是从 2018 年 3 月 11 日到 2018 年 4 月 10 日,依此类推。此人的详细信息需要出现在客户保留表的 2018 年 2 月同类群组中。

将不胜感激任何帮助!谢谢。

【问题讨论】:

  • 您能否添加一些有关如何计算百分比的详细信息?从你的问题看不清楚。例如,是否是该月发生的客户总交易数?
  • 第一行应理解为:在 2018 年 1 月(定义为“Jan Activation Cohort”)进行首次交易的所有客户中,35% 随后在第一个交易日后一个月,下个月 23%,下个月 15%,依此类推。
  • @kdq0 嘿,我编辑了帖子

标签: sql presto


【解决方案1】:

我并不完全熟悉 Presto,也没有办法测试 Presto 代码。但是,通过搜索一下,从 SQL Server 语法之类的东西转换为 Presto 语法似乎并不难。这是我将在 SQL Server 中执行的操作,您应该能够将这个概念带到 Presto:

with transactions_info_per_user as (
select user_id, min(transaction_date) as first_transaction, 
       convert(datepart(year, min(transaction_date)) as varchar(4)) + convert(datepart(month, min(transaction_date)) as varchar(2)) as activation_cohort
from my_table
group by user_id
),

users_per_activation_cohort as (
select activation_cohort, count(*) as number_of_users
from transactions_info_per_user
group by activation_cohort
),

months_after_activation_per_purchase as (
select distinct mt.user_id, ti.activation_cohort, datediff(month, mt.transaction_date, ti.first_transaction) AS months_after_activation
from my_table mt
left join transactions_info_per_user as ti
on mt.user_id = ti.user_id
),

final as (
select activation_cohort, months_after_activation, count(*) as user_count_per_cohort_with_purchase_per_month_after_activation
from months_after_activation_per_purchase 
group by activation_cohort, months_after_activation
)

select activation_cohort, months_after_activation, 
       convert(user_count_per_cohort_with_purchase_per_month_after_activation as decimal(9,2)) / convert(users_per_activation_cohort as decimal(9,2)) * 100
from final

--Then pivot months_after_activation into columns 

我对事物的命名非常明确,因此您可以遵循思考过程。 Here 是如何在 Presto 中进行旋转的示例。希望这对您有所帮助!

【讨论】:

  • 非常感谢。正是我想要的。我刚刚开始学习 sql,所以这非常有帮助:) @kdq0
  • 太棒了,很高兴我能帮上忙!干杯!
【解决方案2】:

您可以使用条件聚合。但是,我不确定你的真实计算是什么。

如果我只使用date_diff()的内置定义,那么逻辑如下:

select date_trunc(month, first_td) as yyyymm,
       count(distinct user_id) as cnt,
       (count(distinct case when date_diff(month, first_td, transaction_date) = 1
                            then user_id
                       end) /
        count(distinct user_id)
       ) as month_1_ratio,
       (count(distinct case when date_diff(month, first_td, transaction_date) = 2
                            then user_id
                       end) /
        count(distinct user_id)
       ) as month_2_ratio
from (select t.*,
             min(transaction_date) over (partition by user_id) as first_td
      from t
     ) t
group by date_trunc(month, first_td)
order by yyyymm;

【讨论】:

  • @ordon Linoff 感谢您提供另一种方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 2015-05-12
相关资源
最近更新 更多