Consecutive Transactions with Increasing Amounts - Problem
Imagine you're a financial analyst tracking customer spending patterns to identify potential high-value customers. You need to find customers who demonstrate consistent growth in their spending over consecutive days.
Problem: Given a Transactions table with customer transactions, find customers who have made transactions with increasing amounts for at least 3 consecutive days.
Table Schema:
Transactions+------------------+------+
| Column Name | Type |
+------------------+------+
| transaction_id | int |
| customer_id | int |
| transaction_date | date |
| amount | int |
+------------------+------+
Key Rules:
- Each (customer_id, transaction_date) combination is unique
- Consecutive means consecutive calendar days
- Amounts must be strictly increasing
- Minimum 3 consecutive days required
Output: Return customer_id, start_date, and end_date of each consecutive period, ordered by customer_id, start_date, end_date.
Input & Output
example_1.sql โ Basic Consecutive Increasing
$
Input:
Transactions table:
| transaction_id | customer_id | transaction_date | amount |
|---------------|-------------|------------------|--------|
| 1 | 101 | 2023-05-01 | 100 |
| 2 | 101 | 2023-05-02 | 150 |
| 3 | 101 | 2023-05-03 | 200 |
| 4 | 102 | 2023-05-01 | 50 |
| 5 | 102 | 2023-05-02 | 25 |
โบ
Output:
| customer_id | consecutive_start | consecutive_end |
|-------------|-------------------|------------------|
| 101 | 2023-05-01 | 2023-05-03 |
๐ก Note:
Customer 101 has consecutive transactions on 05-01 ($100), 05-02 ($150), 05-03 ($200) with increasing amounts. Customer 102 doesn't qualify as amounts decrease from $50 to $25.
example_2.sql โ Multiple Consecutive Periods
$
Input:
Transactions table:
| transaction_id | customer_id | transaction_date | amount |
|---------------|-------------|------------------|--------|
| 1 | 201 | 2023-06-01 | 100 |
| 2 | 201 | 2023-06-02 | 120 |
| 3 | 201 | 2023-06-03 | 140 |
| 4 | 201 | 2023-06-05 | 200 |
| 5 | 201 | 2023-06-06 | 250 |
| 6 | 201 | 2023-06-07 | 300 |
โบ
Output:
| customer_id | consecutive_start | consecutive_end |
|-------------|-------------------|------------------|
| 201 | 2023-06-01 | 2023-06-03 |
| 201 | 2023-06-05 | 2023-06-07 |
๐ก Note:
Customer 201 has two separate consecutive increasing periods: 06-01 to 06-03 (3 days) and 06-05 to 06-07 (3 days), with a gap on 06-04.
example_3.sql โ Edge Case: Exactly 3 Days
$
Input:
Transactions table:
| transaction_id | customer_id | transaction_date | amount |
|---------------|-------------|------------------|--------|
| 1 | 301 | 2023-07-01 | 80 |
| 2 | 301 | 2023-07-02 | 90 |
| 3 | 301 | 2023-07-03 | 100 |
| 4 | 301 | 2023-07-04 | 95 |
| 5 | 302 | 2023-07-01 | 100 |
| 6 | 302 | 2023-07-02 | 110 |
โบ
Output:
| customer_id | consecutive_start | consecutive_end |
|-------------|-------------------|------------------|
| 301 | 2023-07-01 | 2023-07-03 |
๐ก Note:
Customer 301 qualifies with exactly 3 consecutive increasing days, then amount drops on 07-04. Customer 302 only has 2 consecutive days, which doesn't meet the minimum requirement of 3.
Visualization
Tap to expand
Understanding the Visualization
1
Sort by Timeline
Arrange all transactions chronologically for each customer
2
Compare with Yesterday
For each transaction, check if it's consecutive and amount increased from previous day
3
Track Streak Boundaries
Mark where increasing streaks start and end
4
Group Consecutive Days
Assign unique IDs to each streak of consecutive increasing days
5
Filter Valid Patterns
Keep only streaks with 3 or more consecutive increasing days
Key Takeaway
๐ฏ Key Insight: Use LAG() window function to compare consecutive days, then employ running sum technique to group consecutive increasing streaks efficiently in a single SQL pass.
Time & Space Complexity
Time Complexity
O(n log n)
Single sort by customer and date, then linear pass with window functions
โก Linearithmic
Space Complexity
O(n)
Space for final result set and window function intermediate calculations
โก Linearithmic Space
Constraints
- 1 โค Number of transactions โค 104
- 1 โค customer_id โค 1000
- 1 โค amount โค 106
- Each (customer_id, transaction_date) pair is unique
- transaction_date is a valid date in format YYYY-MM-DD
- Amounts must be strictly increasing (not equal)
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code