Given:
timestamp | col1 | col2
============================
xx abc 5
yy abc 4
zz def 7
rr def 6
SELECT timestamp,col1,min(col2)
FROM table
GROUP BY col1
ORDER BY min(col2) ASC
returns:
xx abc 4
zz def 6
timestamp seems to be messed up, so I am sure I am not using group by the way it is supposed to. How do I get:
yy abc 4
rr def 6
For guaranteed behaviour you must either have the timestamp field in an aggregate expression or in the GROUP BY clause. If you don’t (as in your example), the returned value is indeterminent (and effectively almost randomly chosen).
Instead, you can do this in two steps, and so never have any field ‘randonly’ chosen…