Loading problem...
A revenue analytics team maintains transaction-level sales data and needs a yearly growth ledger for each product.
Table: user_transactions
Definitions:
Formula:
Null and safety rules:
Task: Return one row per (product_id, year) that appears in the data.
Output columns (exact order):
Final ordering:
Supported submission environments:
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 |
| 1322 | 123424 | 2145.32 | 2022-12-31 12:00:00 |[
{"year":2019,"product_id":123424,"curr_year_spend":1500.6,"prev_year_spend":null,"yoy_rate":null},
{"year":2020,"product_id":123424,"curr_year_spend":1000.2,"prev_year_spend":1500.6,"yoy_rate":-33.35},
{"year":2021,"product_id":123424,"curr_year_spend":1246.44,"prev_year_spend":1000.2,"yoy_rate":24.62},
{"year":2022,"product_id":123424,"curr_year_spend":2145.32,"prev_year_spend":1246.44,"yoy_rate":72.12}
]Each row compares a product-year total against the immediate previous year for the same product.
user_transactions:
| transaction_id | product_id | spend | transaction_date |
|----------------|------------|--------|-----------------------|
| 1 | 9100 | 500.00 | 2020-01-10 09:00:00 |
| 2 | 9100 | 750.00 | 2022-05-04 11:30:00 |
| 3 | 9200 | 400.00 | 2021-03-03 15:20:00 |
| 4 | 9200 | 0.00 | 2022-08-21 08:10:00 |
| 5 | 9200 | 200.00 | 2023-02-14 13:00:00 |[
{"year":2020,"product_id":9100,"curr_year_spend":500.0,"prev_year_spend":null,"yoy_rate":null},
{"year":2022,"product_id":9100,"curr_year_spend":750.0,"prev_year_spend":null,"yoy_rate":null},
{"year":2021,"product_id":9200,"curr_year_spend":400.0,"prev_year_spend":null,"yoy_rate":null},
{"year":2022,"product_id":9200,"curr_year_spend":0.0,"prev_year_spend":400.0,"yoy_rate":-100.0},
{"year":2023,"product_id":9200,"curr_year_spend":200.0,"prev_year_spend":0.0,"yoy_rate":null}
]A missing intermediate year does not backfill from older years, and division by zero is not allowed.
user_transactions:
| transaction_id | product_id | spend | transaction_date |
|----------------|------------|-------|-----------------------|
| 100 | 8001 | 5.00 | 2021-01-01 10:00:00 |
| 101 | 8001 | 7.00 | 2021-06-01 09:00:00 |
| 102 | 8001 | 9.00 | 2022-06-15 12:00:00 |
| 103 | 8001 | 0.00 | 2022-11-20 08:00:00 |
| 104 | 8002 | 0.00 | 2020-05-05 07:00:00 |
| 105 | 8002 | 75.00 | 2021-05-05 07:00:00 |[
{"year":2021,"product_id":8001,"curr_year_spend":12.0,"prev_year_spend":null,"yoy_rate":null},
{"year":2022,"product_id":8001,"curr_year_spend":9.0,"prev_year_spend":12.0,"yoy_rate":-25.0},
{"year":2020,"product_id":8002,"curr_year_spend":0.0,"prev_year_spend":null,"yoy_rate":null},
{"year":2021,"product_id":8002,"curr_year_spend":75.0,"prev_year_spend":0.0,"yoy_rate":null}
]Spend is aggregated by year first, then growth is computed from yearly totals.
Constraints