Customers With Strictly Increasing Purchases - Problem
In the competitive world of e-commerce, understanding customer purchasing patterns is crucial for business growth. Your task is to identify loyal customers with consistently increasing spending habits - those golden customers who increase their yearly purchases without fail!
You're given an Orders table with the following structure:
| Column Name | Type |
|---|---|
| order_id | int |
| customer_id | int |
| order_date | date |
| price | int |
Your mission: Find customers whose total yearly purchases are strictly increasing from their first order year to their last order year.
Key Rules:
- If a customer didn't order anything in a year within their active period, consider their spending as
0for that year - The analysis period starts from the year of their first order and ends at the year of their last order
- "Strictly increasing" means each year must have higher spending than the previous year (no equal amounts allowed)
Input & Output
basic_case.sql โ Basic Example
$
Input:
Orders table:
| order_id | customer_id | order_date | price |
|----------|-------------|------------|-------|
| 1 | 1 | 2019-07-01 | 1000 |
| 2 | 2 | 2018-11-07 | 800 |
| 3 | 3 | 2019-05-03 | 1500 |
| 4 | 1 | 2019-11-02 | 1500 |
| 5 | 2 | 2018-11-30 | 700 |
| 6 | 3 | 2020-06-18 | 1800 |
| 7 | 1 | 2020-05-25 | 2000 |
| 8 | 2 | 2019-01-11 | 900 |
โบ
Output:
| customer_id |
|-------------|
| 1 |
๐ก Note:
Customer 1: 2019($2500) < 2020($2000) โ Not strictly increasing. Customer 2: 2018($1500) < 2019($900) โ Decreasing. Customer 3: Only one year of data. Customer 1 has strictly increasing purchases: 2019: $2500, 2020: $2000 - Wait, this should be โ. Let me recalculate: Customer 1: 2019: $2500 (orders 1+4), 2020: $2000 (order 7) - Not increasing!
gap_year_case.sql โ Gap Year Handling
$
Input:
Orders table:
| order_id | customer_id | order_date | price |
|----------|-------------|------------|-------|
| 1 | 1 | 2018-01-01 | 1000 |
| 2 | 1 | 2020-01-01 | 2000 |
| 3 | 2 | 2018-01-01 | 500 |
| 4 | 2 | 2019-01-01 | 1000 |
| 5 | 2 | 2020-01-01 | 1500 |
โบ
Output:
| customer_id |
|-------------|
| 2 |
๐ก Note:
Customer 1: 2018($1000) > 2019($0) - not increasing due to gap year with $0. Customer 2: 2018($500) < 2019($1000) < 2020($1500) - strictly increasing every year.
edge_case.sql โ Single Year Customers
$
Input:
Orders table:
| order_id | customer_id | order_date | price |
|----------|-------------|------------|-------|
| 1 | 1 | 2019-01-01 | 1000 |
| 2 | 1 | 2019-06-01 | 500 |
| 3 | 2 | 2018-01-01 | 2000 |
| 4 | 3 | 2020-01-01 | 1000 |
| 5 | 3 | 2021-01-01 | 2000 |
โบ
Output:
| customer_id |
|-------------|
| 3 |
๐ก Note:
Customer 1: Only 2019 data - excluded (need multiple years). Customer 2: Only 2018 data - excluded. Customer 3: 2020($1000) < 2021($2000) - strictly increasing across two years.
Visualization
Tap to expand
Understanding the Visualization
1
Collect Purchase Data
Gather all orders and group by customer and year, calculating yearly totals
2
Fill Timeline Gaps
For each customer, create complete year sequence from first to last order year, filling gaps with $0
3
Compare Year-over-Year
Use LAG() window function to compare each year's spending with the previous year
4
Filter Increasing Patterns
Select only customers where every year shows higher spending than the previous year
Key Takeaway
๐ฏ Key Insight: Use SQL window functions with LAG() to compare consecutive years efficiently, combined with recursive CTEs to handle missing years as $0 spending.
Time & Space Complexity
Time Complexity
O(n log n)
Single pass with sorting for window functions
โก Linearithmic
Space Complexity
O(n)
Space for window function calculations and CTEs
โก Linearithmic Space
Constraints
- 1 โค orders.length โค 104
- 1 โค customer_id โค 103
- 1 โค price โค 106
- order_date is a valid date between 2010-01-01 and 2023-12-31
- All order_id values are unique
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code