Loading problem...
An analytics warehouse tracks product pricing windows and sale events.
Table: prices
Data rule:
Table: units_sold
Task: For each product in prices, compute its weighted average selling price over matched sale events. A sale event matches a price row only when:
Metric definition per product:
Edge behavior:
Output requirements:
Supported submission environments:
prices:
| product_id | start_date | end_date | price |
|------------|------------|------------|-------|
| 1 | 2019-02-17 | 2019-02-28 | 5 |
| 1 | 2019-03-01 | 2019-03-22 | 20 |
| 2 | 2019-02-01 | 2019-02-20 | 15 |
| 2 | 2019-02-21 | 2019-03-31 | 30 |
units_sold:
| product_id | purchase_date | units |
|------------|---------------|-------|
| 1 | 2019-02-25 | 100 |
| 1 | 2019-03-01 | 15 |
| 2 | 2019-02-10 | 200 |
| 2 | 2019-03-22 | 30 |[
{"product_id":1,"average_price":6.96},
{"product_id":2,"average_price":16.96}
]Product 1 average is ((1005)+(1520))/115 = 6.9565..., rounded to 6.96. Product 2 average is ((20015)+(3030))/230 = 16.9565..., rounded to 16.96.
prices:
| product_id | start_date | end_date | price |
|------------|------------|------------|-------|
| 10 | 2024-01-01 | 2024-01-31 | 12 |
| 10 | 2024-02-01 | 2024-02-29 | 20 |
| 11 | 2024-01-01 | 2024-03-31 | 7 |
| 12 | 2024-01-15 | 2024-02-15 | 50 |
units_sold:
| product_id | purchase_date | units |
|------------|---------------|-------|
| 10 | 2024-01-10 | 10 |
| 10 | 2024-02-10 | 5 |
| 10 | 2023-12-31 | 99 |
| 12 | 2024-02-15 | 3 |[
{"product_id":10,"average_price":14.67},
{"product_id":11,"average_price":0.0},
{"product_id":12,"average_price":50.0}
]The 2023-12-31 sale for product 10 is outside every price window and is ignored. Product 11 has no matched sales, so it returns 0.
prices:
| product_id | start_date | end_date | price |
|------------|------------|------------|-------|
| 21 | 2025-04-01 | 2025-04-15 | 2 |
| 21 | 2025-04-16 | 2025-04-30 | 3 |
| 22 | 2025-04-01 | 2025-04-30 | 10 |
units_sold:
| product_id | purchase_date | units |
|------------|---------------|-------|
| 21 | 2025-04-05 | 15 |
| 21 | 2025-04-20 | 25 |
| 22 | 2025-04-10 | 4 |[
{"product_id":21,"average_price":2.63},
{"product_id":22,"average_price":10.0}
]For product 21, weighted average = (215 + 325) / 40 = 2.625, which rounds to 2.63.
Constraints