I'm querying a very large activity log in a MySQL database, trying to figure out what each user was doing at midnight on a particular day. The catch is that I'm only interested in a subset of possible log messages, and I want the last log entry in that subset prior to midnight. Here's what I have so far:
SELECT user, dateAndTime, msg
FROM Log
WHERE msg in ('off', 'on', 'sleep', 'wake')
AND logDate = '2011-12-31';
ORDER BY user ASC, dateAndTime DESC
(We have user and msg as type VARCHAR
, logDate as DATE
, and dateAndTime as DATETIME
.)
This seems like a good first step. The results appear in groups by user name and sorted by time. Is there a way to pull off just the first row of those results for each user? I could always post-process the results, but this seems like something I should be able to do with the query itself. I'm pretty new to thinking in sets instead of procedurally, so I apologize in advance if the solution is supposed to be obvious.
Additional notes...
I've found many other questions on SO that deal with this same kind of problem, but my extra constraints are making it more difficult. I tried modifying this query, but I think the GROUP BY
clause somehow lost the restriction on msg
.
I'm also working under a time constraint. The queries are going through a web service (that I don't control) that automatically times out after 5 minutes. The Log table is big (almost 2 million rows just for 2011-12-31), so most of the solutions I tried involving joins just timed out.
UPDATE:
I discovered something else about the data I previously didn't know. There are multiple rows per user for a given dateAndTime
.