I have a table with prices per products that could be updated daily.
The update process is on-event, so if the price does not change there are no new entries for that day and that product.
I need to calculate the average last 30 days price per each product considering the day without a price equal to the previous last valorized day.
For instance, if there is a new price on 5/12 and another one on 20/12 from 5 to 19 December the price shall be considered equal to the one saved on 5/12.
Any simple good idea to do it with a MySQL query?
For instance, I can enrich the explanation with this example:
Table name: product_prices
Fields: id (PRIMARY KEY), product_ID (external key); product_price (FLOAT); creation (DATETIME)
Table data
---------------------------------------------------------
|id | product_id | product_price | creation |
----------------------------------------------------------
|1 | 30 | 15.2 | 1/12 |
|2 | 53 | 7.5 | 1/12 |
|3 | 30 | 15.4 | 5/12 |
|4 | 30 | 16 | 20/12 |
----------------------------------------------------------
at 31/12 the average price for product_id == 30 shall be (3*15.2+15*15.4+12*16) = 15.62