-1

目前我的交易表有每个月的客户交易数据。Account_ID 标识客户的 ID。Order_ID 是客户已下订单的数量。Reporting_week_start_date 是从星期一开始的每笔交易发生的星期(Date_Purchased)。

每次交易完成后,如何创建一个新表来识别 customer_status?请注意,尽管没有进行任何交易,但新表的 Reporting_week_start_date 直到当前日期。

客户状态

- 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
4

1 回答 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;

这些不完全是您指定的规则。但它们似乎对您想要做的事情进行了非常合理的解释。

于 2020-01-08T11:34:57.623 回答