Forum Moderators: open
items
-----
itemid (int), price (float), pricedate (timestamp), field1, field2, aso
and I want to select the price and date of each item - one price/date for each item each month - more specifically the price and date each month the day the price is the lowest. So I though I'd go for a grouped query and did:
SELECT min(price) as minprice, pricedate, item
FROM items
GROUP BY date_format(pricedate, '%Y%m')
And I thought that was it - it gives me the following:
item ¦ price ¦ date
-------------------------
1 ¦ 100 ¦ 2005-10-01
1 ¦ 69 ¦ 2005-11-02
2 ¦ 765 ¦ 2005-10-01
2 ¦ 45 ¦ 2005-11-01
And so on. The price is indeed the lowest one - only the date is wrong (also, any other fields I include in the SELECT part seems to be randomly chosen - not the actual ones from the row with the lowest price). It just selects the first available date (day) each month.
How can I make this query select fields that actually belong to the row with the lowest price? Any help would be greatly appreciated, this is toying with my SQL competance level... :)
[webmasterworld.com...]
It would look something like this:
SELECT item, price as minprice, pricedate
FROM items AS i1
WHERE price = (SELECT MIN(price)
FROM items AS i2
WHERE i1.item = i2.item AND
date_format(i1.pricedate, '%Y%m') =
date_format(i2.pricedate, '%Y%m')
)
ORDER BY 1,2,3