重复购买相同产品的客户交易

时间:2019-04-08 16:09:01

标签: mysql sql mysql-5.5

与这个问题非常相似:https://docs.aws.amazon.com/transfer/latest/userguide/sftp.ug.pdf#page=28&zoom=100,0,776,但对于MySQL(版本5.5.56-MariaDB,其中窗口函数为NA)

我需要2个额外的列标志,标识为:

  • 该人是否在购买日期之前预订了相同的路线。
  • 该人是否在购买日期之前购买了一条新的不同路线(他们从未购买过的路线)

输出应包含5列:

  1. 电子邮件
  2. 路线
  3. TransactionDate
  4. SameRoute(0 =否,1 =是)
  5. NewRoute(0 =否,1 =是)

伪数据也可以在该问题中找到。

我的尝试

select br.email
, (select count(*) 
     from bookings r1
    where r1.Email = br.Email 
      and r1.id < br.id
   )  as  repeat_rank
, (select count(*) 
     from bookings r2
    where r2.Email = br.Email 
      and r2.id < br.id
      and r2.OutBoundRoute = br.OutBoundRoute
   )  as same_route_repeat_rank
, (select count(*) 
     from bookings r3
    where r3.Email = br.Email 
      and r3.id < br.id
      and r3.OutBoundRoute <> br.OutBoundRoute
   )  as new_route_repeat_rank
, *
from  bookings br
where br.TransactionDate < current_date

此查询的问题是,如果先前的购买路线不同,则same_route_repeat_rank会重置为0,但如果客户在过去的任何时间购买了相同的路线,我希望它带回数字> 0

0 个答案:

没有答案