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_id ascending

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
Consecutive Days Analysis Workflow1. Raw DataCustomer transactionswith dates2. Sort & NumberROW_NUMBER()within customer3. Group Magicdate - row_numcreates groups4. Count & WinFind maximumconsecutive daysExample: Window Function in ActionBefore (Raw Data):Customer 1: [2024-01-01, 2024-01-02, 2024-01-03]Customer 2: [2024-01-01, 2024-01-03, 2024-01-04]After ROW_NUMBER() and Date Grouping:Cust | Date | Row | Group | Consecutive? 1 | 2024-01-01 | 1 | 2024-01-01 | โœ“ (3 days) 1 | 2024-01-02 | 2 | 2024-01-01 | โœ“ 1 | 2024-01-03 | 3 | 2024-01-01 | โœ“ 2 | 2024-01-01 | 1 | 2024-01-01 | โœ— (max 2) 2 | 2024-01-03 | 2 | 2024-01-02 | โœ— 2 | 2024-01-04 | 3 | 2024-01-02 | โœ—๐ŸŽฏ Result: Customer 1 wins with 3 consecutive days!Same date_group = consecutive sequence
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ยณ)!
Asked in
Google 42 Amazon 38 Meta 29 Microsoft 25
52.6K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen