Year on Year Growth Rate - Problem
Year-on-Year Growth Rate Analysis
You are working as a data analyst for an e-commerce company that tracks product performance over multiple years. Your task is to calculate the year-on-year (YoY) growth rate for each product's total spending to help the business understand which products are growing or declining.
Given a table
1. Aggregate total spending by product and year
2. Calculate the YoY growth rate using the formula:
3. Handle edge cases where no previous year data exists (return NULL)
The growth rate helps identify:
โข ๐ Growing products (positive YoY rate)
โข ๐ Declining products (negative YoY rate)
โข ๐ New products (NULL for first year)
Key Requirements:
โข Round growth rates to 2 decimal places
โข Order results by
โข Include NULL values for first year of each product
You are working as a data analyst for an e-commerce company that tracks product performance over multiple years. Your task is to calculate the year-on-year (YoY) growth rate for each product's total spending to help the business understand which products are growing or declining.
Given a table
user_transactions containing transaction data, you need to:1. Aggregate total spending by product and year
2. Calculate the YoY growth rate using the formula:
((current_year - previous_year) / previous_year) ร 1003. Handle edge cases where no previous year data exists (return NULL)
The growth rate helps identify:
โข ๐ Growing products (positive YoY rate)
โข ๐ Declining products (negative YoY rate)
โข ๐ New products (NULL for first year)
Key Requirements:
โข Round growth rates to 2 decimal places
โข Order results by
product_id and year ascendingโข Include NULL values for first year of each product
Input & Output
basic_growth.sql โ Basic Growth Calculation
$
Input:
user_transactions:
transaction_id | product_id | spend | transaction_date
1341 | 123424 | 1500.60| 2019-12-31 12:00:00
1423 | 123424 | 1000.20| 2020-12-31 12:00:00
1623 | 123424 | 1246.44| 2021-12-31 12:00:00
โบ
Output:
year | product_id | curr_year_spend | prev_year_spend | yoy_rate
2019 | 123424 | 1500.60 | NULL | NULL
2020 | 123424 | 1000.20 | 1500.60 | -33.35
2021 | 123424 | 1246.44 | 1000.20 | 24.62
๐ก Note:
Product 123424 shows declining performance in 2020 (-33.35%) but recovers in 2021 (+24.62%). First year (2019) has NULL growth rate as expected.
multiple_products.sql โ Multiple Products
$
Input:
user_transactions:
transaction_id | product_id | spend | transaction_date
1001 | 111 | 500.00 | 2020-06-15 10:00:00
1002 | 111 | 750.00 | 2021-06-15 10:00:00
1003 | 222 | 1200.00| 2020-03-20 15:00:00
1004 | 222 | 960.00 | 2021-03-20 15:00:00
โบ
Output:
year | product_id | curr_year_spend | prev_year_spend | yoy_rate
2020 | 111 | 500.00 | NULL | NULL
2021 | 111 | 750.00 | 500.00 | 50.00
2020 | 222 | 1200.00 | NULL | NULL
2021 | 222 | 960.00 | 1200.00 | -20.00
๐ก Note:
Two products with opposite trends: Product 111 grows 50%, while Product 222 declines by 20%. Each product's growth is calculated independently.
aggregation_edge_case.sql โ Multiple Transactions Per Year
$
Input:
user_transactions:
transaction_id | product_id | spend | transaction_date
2001 | 333 | 100.00 | 2020-01-15 09:00:00
2002 | 333 | 200.00 | 2020-06-15 14:00:00
2003 | 333 | 150.00 | 2020-12-15 16:00:00
2004 | 333 | 300.00 | 2021-03-10 11:00:00
2005 | 333 | 225.00 | 2021-08-20 13:00:00
โบ
Output:
year | product_id | curr_year_spend | prev_year_spend | yoy_rate
2020 | 333 | 450.00 | NULL | NULL
2021 | 333 | 525.00 | 450.00 | 16.67
๐ก Note:
Multiple transactions per year are correctly aggregated: 2020 total = $450.00 (100+200+150), 2021 total = $525.00 (300+225). Growth rate = 16.67%.
Constraints
- 1 โค number of transactions โค 105
- 1 โค product_id โค 106
- 0.01 โค spend โค 106.00
- transaction_date format: YYYY-MM-DD HH:MM:SS
- Growth rates must be rounded to 2 decimal places
- Results ordered by product_id ASC, year ASC
Visualization
Tap to expand
Understanding the Visualization
1
Raw Transaction Data
Individual transactions scattered across different dates and products
2
Annual Aggregation
Group transactions by product and year, sum up total spending
3
Window Function Magic
LAG() function looks back to previous year within each product partition
4
Growth Rate Calculation
Apply percentage growth formula: ((current - previous) / previous) ร 100
Key Takeaway
๐ฏ Key Insight: Window functions like LAG() transform complex multi-pass problems into elegant single-pass solutions, making time-series analysis both efficient and intuitive.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code