如何从交易数据创建流失表?

问题描述 投票:0回答:1

当前,我的交易表具有每个月客户的交易数据。 Account_ID标识客户的ID。 Order_ID是客户已下订单的数量。 Reporting_week_start_date是从星期一开始的一周,进行每笔交易(购买日期)。

每笔交易完成后,如何创建新表来标识customer_status?请注意,尽管未进行任何交易,但新表的Reporting_week_start_date到当前日期为止。

Customer_Status

- New : customers who made their first paid subscription
- Recurring :  customers with continuous payment
- Churned :  when customers' subscriptions had expired and there's no renewal within the next month/same month
- Reactivated : customers who had churned and then returned to re-subscribe

交易表

Account_ID  | Order_ID |  Reporting_week_start_date| Date_Purchased | Data_Expired
    001     | 1001     |       31 Dec 2018         |   01 Jan 2019  |    08 Jan 2019
    001     | 1001     |       07 Jan 2019         |   08 Jan 2019  |    15 Jan 2019
    001     | 1001     |       14 Jan 2019         |   15 Jan 2019  |    22 Jan 2019    #Transaction 1
    001     | 1001     |       21 Jan 2019         |   22 Jan 2019  |    29 Jan 2019
    001     | 1001     |       28 Jan 2019         |   29 Jan 2019  |    31 Jan 2019

    001     | 1002     |       28 Jan 2019         |   01 Feb 2019  |    08 Feb 2019
    001     | 1002     |       04 Feb 2019         |   08 Feb 2019  |    15 Feb 2019    #Transaction 2
    001     | 1002     |       11 Feb 2019         |   15 Feb 2019  |    22 Feb 2019
    001     | 1002     |       18 Feb 2019         |   22 Feb 2019  |    28 Feb 2019

    001     | 1003     |       25 Feb 2019         |   01 Mar 2019  |    08 Mar 2019
    001     | 1003     |       04 Mar 2019         |   08 Mar 2019  |    15 Mar 2019
    001     | 1003     |       11 Mar 2019         |   15 Mar 2019  |    22 Mar 2019    #Transaction 3
    001     | 1003     |       18 Mar 2019         |   22 Mar 2019  |    29 Mar 2019
    001     | 1003     |       25 Mar 2019         |   29 Mar 2019  |    31 Mar 2019

    001     | 1004     |       27 May 2019         |   01 Jun 2019  |    08 Jun 2019
    001     | 1004     |       03 Jun 2019         |   08 Jun 2019  |    15 Jun 2019    #Transaction 4
    001     | 1004     |       10 Jun 2019         |   15 Jun 2019  |    22 Jun 2019
    001     | 1004     |       17 Jun 2019         |   22 Jun 2019  |    29 Jun 2019
    001     | 1004     |       24 Jun 2019         |   29 Jun 2019  |    30 Jun 2019

预期输出

Account_ID  | Order_ID |  Reporting_week_start_date| Customer_status
    001     | 1001     |       31 Dec 2018         |   New  
    001     | 1001     |       07 Jan 2019         |   New               #Transaction 1
    001     | 1001     |       14 Jan 2019         |   New
    001     | 1001     |       21 Jan 2019         |   New  
    001     | 1001     |       28 Jan 2019         |   New  

    001     | 1002     |       28 Jan 2019         |   Recurring        
    001     | 1002     |       04 Feb 2019         |   Recurring         #Transaction 2
    001     | 1002     |       11 Feb 2019         |   Recurring  
    001     | 1002     |       18 Feb 2019         |   Recurring  

    001     | 1003     |       25 Feb 2019         |   Churned  
    001     | 1003     |       04 Mar 2019         |   Churned           #Transaction 3
    001     | 1003     |       11 Mar 2019         |   Churned      
    001     | 1003     |       18 Mar 2019         |   Churned    
    001     | 1003     |       25 Mar 2019         |   Churned    

    001     |    -     |       1 Apr 2019          |   Churned  
    001     |    -     |       08 Apr 2019         |   Churned    
    001     |    -     |       15 Apr 2019         |   Churned      
    001     |    -     |       22 Apr 2019         |   Churned    
    001     |    -     |       29 Apr 2019         |   Churned

    001     |    -     |       29 Apr 2019         |   Churned  
    001     |    -     |       06 May 2019         |   Churned    
    001     |    -     |       13 May 2019         |   Churned      
    001     |    -     |       20 May 2019         |   Churned    
    001     |    -     |       27 May 2019         |   Churned

    001     | 1004     |       27 May 2019         |   Reactivated  
    001     | 1004     |       03 Jun 2019         |   Reactivated       #Transaction 4
    001     | 1004     |       10 Jun 2019         |   Reactivated  
    001     | 1004     |       17 Jun 2019         |   Reactivated  
    001     | 1004     |       24 Jun 2019         |   Reactivated'
    ...
    ...
    ...
    current date
sql google-bigquery lag lead churn
1个回答
0
投票

我认为您只需要窗口函数和case逻辑。假设您所指的日期为Reporting_week_start_date,则逻辑如下所示:

select t.*,
       (case when Reporting_week_start_date = min(Reporting_week_start_date) over (partition by account_id)
             then 'New'
             when Reporting_week_start_date < dateadd(lag(Reporting_week_start_date) over (partition by account_id order by Reporting_week_start_date), interval 1 month)
             then 'Recurring'
             when Reporting_week_start_date < dateadd(lead(Reporting_week_start_date) over (partition by account_id order by Reporting_week_start_date), interval -1 month)
             then 'Churned'
             else 'Reactivated'
        end) as status
from transactions t;

这些不是确切地您指定的规则。但是它们似乎对您想做什么很合理。

© www.soinside.com 2019 - 2024. All rights reserved.