+------------------+----------+
| Column Name | Type |
+------------------+----------+
| transaction_id | integer |
| product_id | integer |
| spend | decimal |
| transaction_date | datetime |
+------------------+----------+
The transaction_id column uniquely identifies each row in this table.
Each row of this table contains the transaction ID, product ID, the spend amount, and the transaction date.
Write a solution to calculate the year-on-year growth rate for the total spend for each product.
The result table should include the following columns:
year: The year of the transaction.
product_id: The ID of the product.
curr_year_spend: The total spend for the current year.
prev_year_spend: The total spend for the previous year.
yoy_rate: The year-on-year growth rate percentage, rounded to 2 decimal places.
Return the result table ordered byproduct_id,yearinascending order.
We need to compute the total spend for each product and year, then compare each year’s spend to the previous year’s spend for the same product. This is a classic use case for window functions (SQL) or groupby/shift (Pandas).
SELECTyear, product_id,
curr_year_spend,
prev_year_spend,
ROUND((curr_year_spend - prev_year_spend) / prev_year_spend *100, 2) AS yoy_rate
FROM (
SELECTYEAR(transaction_date) ASyear,
product_id,
SUM(spend) AS curr_year_spend,
LAG(SUM(spend)) OVER (PARTITION BY product_id ORDERBYYEAR(transaction_date)) AS prev_year_spend
FROM user_transactions
GROUPBY product_id, YEAR(transaction_date)
) t
ORDERBY product_id, year;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SELECTyear, product_id,
curr_year_spend,
prev_year_spend,
ROUND((curr_year_spend - prev_year_spend) / prev_year_spend *100.0, 2) AS yoy_rate
FROM (
SELECTEXTRACT(YEARFROM transaction_date) ASyear,
product_id,
SUM(spend) AS curr_year_spend,
LAG(SUM(spend)) OVER (PARTITION BY product_id ORDERBYEXTRACT(YEARFROM transaction_date)) AS prev_year_spend
FROM user_transactions
GROUPBY product_id, EXTRACT(YEARFROM transaction_date)
) t
ORDERBY product_id, year;