【问题标题】:I want find customers transacting for any consecutive 3 months from year 2017 to 2018我想找到从 2017 年到 2018 年连续 3 个月进行交易的客户
【发布时间】:2019-11-23 21:03:32
【问题描述】:

我想知道找到连续 3 个月交易的客户列表的诀窍,这可以是任意连续 3 个月且发生次数不限。

示例:假设有客户在 1 月份进行交易,然后一直交易到 3 月,然后他停止了交易。我想从我的数据库中获取这些客户的列表。

我正在开发 AWS Athena。

【问题讨论】:

  • 请提供样本数据和预期结果,以及您当前解决问题的尝试。

标签: sql database date amazon-athena monthcalendar


【解决方案1】:

一种方法使用聚合和窗口函数:

select customer_id, yyyymm_2
from (select date_trunc(month, transactdate) as yyyymm, customer_id,
             lag(date_trunc(month, transactdate), 2) over (partition by customer_id order by date_trunc(month, transactdate)) as prev_yyyymm_2
      from t
      where transactdate >= '2017-01-01' and
            transactadte < '2019-01-01'
     )
where prev_dt_2 = yyyymm - interval '2' month;

这会按月汇总交易并查看前两行的交易日期。外部过滤器检查该日期是否正好早 2 个月。

【讨论】:

    猜你喜欢
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    相关资源
    最近更新 更多