Approach
Relational aggregation
For Consecutive Transactions with Increasing Amounts, the query transforms and combines relational rows, then filters or aggregates them into the requested result.
- Identify the source rows and join keys.
- Apply filters before aggregation when possible.
- Group, rank, or project the final columns required by the result.
Code notes
- 37 lines of SQL from the credited upstream file 2701.sql.
- The implementation keeps its working state in language-native values and containers.
- No explicit loop blocks detected.
Complexity
Review join cardinality, grouping keys, and available indexes when estimating query cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1WITH2 IncreasingTransactions AS (3 SELECT4 Curr.customer_id,5 Curr.transaction_date6 FROM Transactions AS Curr7 LEFT JOIN Transactions AS Next8 USING (customer_id)9 WHERE10 Curr.amount < Next.amount11 AND DATEDIFF(Next.transaction_date, Curr.transaction_date) = 112 ),13 IncreasingTransactionsWithGroupId AS (14 SELECT15 *,16 TO_DAYS(transaction_date) - ROW_NUMBER() OVER(17 PARTITION BY customer_id18 ORDER BY transaction_date19 ) AS group_id20 FROM IncreasingTransactions21 ),22 IncreasingTransactionsWithCountDays AS (23 SELECT24 customer_id,25 MIN(transaction_date) AS consecutive_start,26 COUNT(*) AS count_days27 FROM IncreasingTransactionsWithGroupId28 GROUP BY customer_id, group_id29 )30SELECT31 customer_id,32 consecutive_start,33 DATE_ADD(consecutive_start, INTERVAL count_days DAY) AS consecutive_end34FROM IncreasingTransactionsWithCountDays35WHERE count_days >= 236ORDER BY 1;37