【问题标题】: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每月显示之前销售日期 > 3 个月的客户数量和给定月份中具有“销售日期”的客户数量
【发布时间】:2020-02-01 19:59:43
【问题描述】:

基本上,我的要求是 - 对于给定月份,有多少客户在给定月份前 3 个月拥有“上一个销售日期”,而这些客户中有多少人在给定月份拥有“销售日期”。

我尝试使用 Lag 函数,但我的列“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"

我的预期输出是每月的“销售日期”在给定月份前 3 个月以上的客人数量(Lost_Guests),以及这些客户中有多少人在给定月份有“销售日期”(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

【问题讨论】:

  • 您使用的是哪个 dbms?
  • 除了数据库标签,请显示示例数据。
  • 我正在连接到 Amazon Redshift 数据库

标签: sql amazon-redshift amazon window-functions lag


【解决方案1】:

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

你似乎想要这样的东西:

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;

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

【讨论】:

  • 感谢戈登的这个解决方案!但是,要求略有变化:按月计算,我正在测量在给定月份重新激活的流失客人的百分比。我已经通过上面的示例详细说明了标准,如果您可以在这里查看并指导我,将会非常有帮助! :) 非常感谢您的帮助
  • @srikarmalladi 。 . .新问题应作为问题提出,而不是通过编辑已回答的问题来提出。
  • 抱歉,我不知道,因为我是平台新手,我会发布一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-20
  • 2015-02-20
  • 1970-01-01
相关资源
最近更新 更多