Skip to content

2701. Consecutive Transactions with Increasing Amounts 👍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
WITH
  IncreasingTransactions AS (
    SELECT
      Curr.customer_id,
      Curr.transaction_date
    FROM Transactions AS Curr
    LEFT JOIN Transactions AS Next
      USING (customer_id)
    WHERE
      Curr.amount < Next.amount
      AND DATEDIFF(Next.transaction_date, Curr.transaction_date) = 1
  ),
  IncreasingTransactionsWithGroupId AS (
    SELECT
      *,
      TO_DAYS(transaction_date) - ROW_NUMBER() OVER(
        PARTITION BY customer_id
        ORDER BY transaction_date
      ) AS group_id
    FROM IncreasingTransactions
  ),
  IncreasingTransactionsWithCountDays AS (
    SELECT
      customer_id,
      MIN(transaction_date) AS consecutive_start,
      COUNT(*) AS count_days
    FROM IncreasingTransactionsWithGroupId
    GROUP BY customer_id, group_id
  )
SELECT
  customer_id,
  consecutive_start,
  DATE_ADD(consecutive_start, INTERVAL count_days DAY) AS consecutive_end
FROM IncreasingTransactionsWithCountDays
WHERE count_days >= 2
ORDER BY 1;