每月显示先前销售日期 > 3 个月的客户数量,以及在给定月份有 "Sale Date" 的客户数量

Display # of customers per month that have previous Sale date > 3 months and # of these customers that have a "Sale Date" in the given month

基本上,我的要求是 - 对于给定的月份,有多少客户在给定月份之前有 "previous Sale date" 3 个月,并且在这些客户中有多少人在给定月份有 "Sale date"月。

我尝试使用滞后函数,但我的专栏 "Reactivated_Guests" 总是给我空值。

 SELECT datepart(month,["sale date"]) `"Sale_Month",count(distinct 
["user id"]) "Lost_Guests",
lag("Guests",4) OVER (ORDER BY "Sale_Month")+
lag("Guests",5) OVER (ORDER BY "Sale_Month")+
lag("Guests",6) OVER (ORDER BY "Sale_Month")+
lag("Guests",7) OVER (ORDER BY "Sale_Month")+
lag("Guests",8) OVER (ORDER BY "Sale_Month")+
lag("Guests",9) OVER (ORDER BY "Sale_Month")+
lag("Guests",10) OVER (ORDER BY "Sale_Month")+
lag("Guests",11) OVER (ORDER BY "Sale_Month")+
lag("Guests",12) OVER (ORDER BY "Sale_Month") "Reactivated_Guests"
group by "Sale_Month"
order by "Sale_Month"

我的预期输出是在给定月份 (Lost_Guests) 之前 "Sale date" 超过 3 个月的客人数量,以及这些客户中有多少 "Sale date" 在给定月份 (Reactivated_Guests)

预期结果:

Sale_Month     Lost_Guests                     Reactivated_Guests
              (prev Sale date > 3 months)  (Prev Sale date > 3 months and 
                                            have a Sale date in given month)
  June         1,200                            110
  July         1,800                            130
  Aug          1,900                            140

实际结果:

Sale_Month     Lost_Guests      Reactivated_Guests

  June         1,200               null
  July         1,800               null
  Aug          1,900               null

示例数据:

Customer      Sale Date
AAAAA        11/15/2018
BBBBB        11/16/2018
CCCCC         9/23/2018
CCCCC         1/25/2019  
AAAAA         3/16/2019    ----> so for given month of March, AAAAA to be 
CCCCC         3/18/2019          considered in "Lost_Guests"  because 
                                 AAAAA's previous sale date (11/15/2018) is
                                 more than 3 months from the given month 
                                (March - 2019) and AAAAA to be considered in 
                                 "Reactivated_guests" because AAAAA has a 
                                 Sale date in the given month (March-2019) 


                           ----> for given month of March, CCCCC shall not 
                                 be considered in "Lost guests" and
                                 "Reactivated Guests" because 
                                 previous sale date (1/25/2019) is less 
                                 than 3 months from given month (March-2019)
                                 and hence does not appear in 
                                 "Reactivated_Guests" as well

这解决了问题的原始版本。

您似乎想要这样的东西:

select sale_month, count(distinct user_id) as guests,
       count(distinct case when min_sale_date < sale_date - interval '3 month' then user_id end) as old_guests
from (select t.*,
             min(sale_date) over (partition by user_id) as min_sale_date
      from t
     ) t
group by sale_month
order by sale_month;

请注意,日期函数非常依赖于数据库,因此具体语法可能因数据库而异。