I am tracking the sales price of various items, and I would like to calculate the average price of each item over a period of time.
My simplified tables look like this:
Items
------------------
|key | title |
------------------
|1 | Sproket 1|
|2 | Sproket 2|
------------------
And
Prices
----------------------------------------
|item_key | price | datetime |
----------------------------------------
|1 | 2.99 | 2014-11-25 02:05:56|
|1 | 1.99 | 2014-12-13 02:05:56|
|2 | 9.99 | 2014-10-25 02:05:56|
|2 | 8.99 | 2014-11-13 02:05:56|
----------------------------------------
The Items table contains a single row for each item. The Prices table contains a single row for each price change. The script runs daily and checks for the current prices, if the current price is different than the most recent item price recorded in Prices than a new entry is made in Prices.
My question is, How do I calculate the average price of an item over 180 days?
I can't use AVG because there is not a price entry each day for each item.
AVG()
of the last price for each day?