ORDER BY MIN()?

admin

Administrator
Staff member
Is there any way to order by the minimum value in a group?

I have a table of page views which has columns for session_id and time (seconds since the epoch). I want to list the session ID's of the most recent 20 sessions (based on the time of their first page request).

The query:

SELECT session_id, MIN(time)
FROM page_views
GROUP BY session_id
ORDER BY MIN(time) DESC
LIMIT 20

...produces an error. If I use ORDER BY time DESC instead of ORDER BY MIN(time) DESC, it seems to work. However, when two sessions overlap, I get inconsistent results.

Is there any way to make it order the groups by the _first_ time recorded (which is the minimum time in the group)?

Thanks.
Keegan
 
Back
Top