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 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) ร— 100
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 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
Year-on-Year Growth Analysis Pipeline1. Raw Transactions2019: $1500.602020: $1000.202021: $1246.442. Annual AggregationGROUP BY year, productSUM(spend) per year3. Window FunctionLAG() gets previous yearPARTITION BY product_idORDER BY year4. Growth Calculation2020: -33.35%2021: +24.62%2022: +72.12%Growth Rate Formula:YoY Rate = ((Current Year Spend - Previous Year Spend) / Previous Year Spend) ร— 100Example for 2021:YoY Rate = ((1246.44 - 1000.20) / 1000.20) ร— 100 = 24.62%โš ๏ธ Handle division by zero and NULL previous years
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.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
67.2K 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