I’m working with stock price data and making this query to get one price per month for, say, Google (from a table with prices for every weekday):
SELECT * FROM pricedata WHERE symbol='GOOG' GROUP BY symbol,month(date),year(date)
2012-07-02
2012-06-01
2012-05-01
If I remove the WHERE clause it returns the last date of the month instead of the first (and more rows because all the symbols in the table show up, of course), as in
SELECT * FROM pricedata GROUP BY symbol,month(date),year(date)
2012-07-31
2012-06-29
2012-05-31
I understand that ‘The server is free to choose any value from each group’, so I’m probably just lucky that the first query works as I want, but I’d like to know why the change occurs.
Is there a fast solution to consistently get the first value of each month? I haven’t found any similar questions, but they might be out there… The second query isn’t vital but I was trying to build a reduced-size table for monthly-only records and ran into this problem.
The server is free to choose any value from the group, but in practice it chooses the first it finds. If you have an index on
symbolthen it will choose the first one as they are sorted in the index. If you remove theWHEREclause it will use another index or the clustered key. This will be sorted in a different order, and therefore the first row the server encounters will be different.Basically you should not ever rely on getting any specific value. As the documentation says, the server is free to choose any value and which value it chooses is not specified.