Customers with Maximum Number of Transactions on Consecutive Days - Problem
You're analyzing customer transaction patterns to identify the most active customers. Given a Transactions table with customer transactions, your goal is to find customers who achieved the maximum number of consecutive transaction days.
Problem: Find all customers who made transactions for the highest number of consecutive days. For example, if one customer made transactions for 5 consecutive days and another for 3 consecutive days, you want the customer with 5 consecutive days (assuming that's the maximum).
Key Points:
- Each row represents a unique
(customer_id, transaction_date)pair - Consecutive days means no gaps between transaction dates
- Return ALL customers tied for the maximum consecutive transaction days
- Results should be ordered by
customer_idascending
This problem tests your ability to work with date sequences, window functions, and finding maximum consecutive sequences in SQL.
Input & Output
example_1.sql โ Basic Case
$
Input:
Transactions table:
| transaction_id | customer_id | transaction_date | amount |
|----------------|-------------|------------------|--------|
| 1 | 1 | 2024-01-01 | 100 |
| 2 | 1 | 2024-01-02 | 150 |
| 3 | 1 | 2024-01-03 | 200 |
| 4 | 2 | 2024-01-01 | 50 |
| 5 | 2 | 2024-01-03 | 75 |
| 6 | 3 | 2024-01-01 | 300 |
| 7 | 3 | 2024-01-02 | 250 |
โบ
Output:
| customer_id |
|-------------|
| 1 |
๐ก Note:
Customer 1 made transactions on 3 consecutive days (2024-01-01, 2024-01-02, 2024-01-03). Customer 2 made transactions on 2 non-consecutive days with a gap. Customer 3 made transactions on 2 consecutive days. The maximum consecutive days is 3, achieved only by Customer 1.
example_2.sql โ Multiple Winners
$
Input:
Transactions table:
| transaction_id | customer_id | transaction_date | amount |
|----------------|-------------|------------------|--------|
| 1 | 1 | 2024-01-01 | 100 |
| 2 | 1 | 2024-01-02 | 150 |
| 3 | 2 | 2024-01-05 | 200 |
| 4 | 2 | 2024-01-06 | 250 |
| 5 | 3 | 2024-01-10 | 300 |
โบ
Output:
| customer_id |
|-------------|
| 1 |
| 2 |
๐ก Note:
Both Customer 1 and Customer 2 have 2 consecutive transaction days each, which is the maximum. Customer 3 only has 1 day. Therefore, both customers 1 and 2 are returned, sorted by customer_id.
example_3.sql โ Single Transaction Days
$
Input:
Transactions table:
| transaction_id | customer_id | transaction_date | amount |
|----------------|-------------|------------------|--------|
| 1 | 1 | 2024-01-01 | 100 |
| 2 | 2 | 2024-01-03 | 150 |
| 3 | 3 | 2024-01-05 | 200 |
โบ
Output:
| customer_id |
|-------------|
| 1 |
| 2 |
| 3 |
๐ก Note:
All customers have exactly 1 transaction day each, so the maximum consecutive days is 1. All customers are tied for the maximum, so all are returned in ascending order of customer_id.
Constraints
- 1 โค number of transactions โค 105
- 1 โค customer_id โค 104
- transaction_date is in format 'YYYY-MM-DD'
- 1 โค amount โค 106
- Each (customer_id, transaction_date) pair is unique
- At least one customer will have at least one transaction
Visualization
Tap to expand
Understanding the Visualization
1
Data Preparation
Extract unique customer-date pairs and sort them chronologically by customer
2
Window Function Magic
Apply ROW_NUMBER() within each customer group to create sequential numbering
3
Date Grouping
Calculate date_group = date - (row_number - 1) to group consecutive sequences
4
Count Sequences
Group by customer and date_group to count consecutive days in each sequence
5
Find Winners
Identify customers with the maximum consecutive transaction days
Key Takeaway
๐ฏ Key Insight: The ROW_NUMBER() window function technique transforms a complex consecutive sequence problem into simple grouping. Consecutive dates have the same `date - row_number` value, making detection O(n log n) instead of O(nยณ)!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code