Weighted Average Sales
Start Timer
0:00:00
The sales department is conducting a performance review and is interested in trends in product sales. They have decided to use a weighted moving average as part of their analysis.
Write a SQL query to calculate the 3-day weighted moving average of sales for each product. Use the weights 0.5 for the current day, 0.3 for the previous day, and 0.2 for the day before that.
Note: Only output the weighted moving average for dates that have two or more preceding dates. You may assume that the table doesn’t skip dates.
Example:
Input:
sales table
| Column | Type |
|---|---|
| date | DATE |
| product_id | INTEGER |
| sales_volume | INTEGER |
Output:
| Column | Type |
|---|---|
| date | DATE |
| product_id | INTEGER |
| weighted_avg_sales | FLOAT |
.
.
.
.
.
.
.
.
.
Comments