Given a table Transactions with columns transaction_id, customer_id, transaction_date, and amount, write an SQL query to find customers who have made consecutive transactions with increasing amounts for at least three consecutive days.
Return:
customer_idconsecutive_start- Start date of the consecutive periodconsecutive_end- End date of the consecutive period
Order results by customer_id, consecutive_start, and consecutive_end in ascending order.
Table Schema
| Column Name | Type | Description |
|---|---|---|
transaction_id
PK
|
int | Primary key - unique transaction identifier |
customer_id
|
int | Customer identifier |
transaction_date
|
date | Date of transaction |
amount
|
int | Transaction amount |
Input & Output
| transaction_id | customer_id | transaction_date | amount |
|---|---|---|---|
| 1 | 101 | 2024-01-01 | 100 |
| 2 | 101 | 2024-01-02 | 150 |
| 3 | 101 | 2024-01-03 | 200 |
| 4 | 101 | 2024-01-04 | 250 |
| 5 | 101 | 2024-01-06 | 120 |
| 6 | 101 | 2024-01-07 | 180 |
| 7 | 101 | 2024-01-08 | 220 |
| 8 | 102 | 2024-01-01 | 300 |
| 9 | 102 | 2024-01-02 | 250 |
| customer_id | consecutive_start | consecutive_end |
|---|---|---|
| 101 | 2024-01-01 | 2024-01-04 |
| 101 | 2024-01-06 | 2024-01-08 |
Customer 101 has two periods of consecutive increasing transactions: from Jan 1-4 (100→150→200→250) and Jan 6-8 (120→180→220). Customer 102 only has 2 consecutive days, which doesn't meet the minimum requirement of 3 days.
| transaction_id | customer_id | transaction_date | amount |
|---|---|---|---|
| 1 | 201 | 2024-01-01 | 100 |
| 2 | 201 | 2024-01-02 | 200 |
| 3 | 201 | 2024-01-03 | 150 |
| 4 | 201 | 2024-01-04 | 300 |
| 5 | 202 | 2024-01-01 | 500 |
| customer_id | consecutive_start | consecutive_end |
|---|
No customers qualify. Customer 201 has only 2 consecutive increasing days (Jan 1-2), then amount decreases on Jan 3. Customer 202 has only 1 transaction. Neither meets the 3+ consecutive days requirement.
| transaction_id | customer_id | transaction_date | amount |
|---|---|---|---|
| 1 | 301 | 2024-01-01 | 50 |
| 2 | 301 | 2024-01-02 | 75 |
| 3 | 301 | 2024-01-03 | 100 |
| 4 | 301 | 2024-01-05 | 200 |
| customer_id | consecutive_start | consecutive_end |
|---|---|---|
| 301 | 2024-01-01 | 2024-01-03 |
Customer 301 has exactly 3 consecutive days with increasing amounts (50→75→100). The transaction on Jan 5 doesn't count as consecutive since Jan 4 is missing.
Constraints
-
1 ≤ transaction_id ≤ 100000 -
1 ≤ customer_id ≤ 1000 -
1 ≤ amount ≤ 100000 -
Each
(customer_id, transaction_date)is unique - At least 3 consecutive days required