Alright I have a table where I need to get the relevant date from a GROUP BY function…
Here is a sample of the data:
min_price, day
140000 2010-09-26
130000 2010-09-27
154991 2010-10-08
143000 2010-10-09
156470 2010-10-10
I would like this result:
130000 2010-09-27
143000 2010-10-09
Here is the SQL I use:
SELECT MIN(min_price) AS low_price, min_price, day
FROM compress
WHERE item_name = '$item'
GROUP BY EXTRACT(MONTH FROM day), EXTRACT(YEAR FROM day)
Here is what I get:
130000 2010-09-26
143000 2010-10-08
As you can see, the dates don’t match up with the min row value. I need them to match up. What SQL can I use to get that result?
ADDITION
MySQL is better with joins so here’s a variant of the same query
What is really slowing down any variation is the calling of Extract on every row. If you had a calendar table containing a row for each date and a column for the month and year of the date and your calendar table covered the date ranges in your data, you could then do something like: