I have a routine that runs every few hours that creates several entries in a table used for logging. What I need to do is select all the records with the most recent timestamp that have a common account id. Something like this:
SELECT *
FROM TABLE_logs
WHERE ACCOUNT_ID='12345'
ORDER BY TIMESTAMP DESC
Where I’m stuck is that I can’t say LIMIT 5 or something like that because the number of records created at each routine interval could be different. So, for example, when the routine runs at 10AM, it may create 6 table entries and only 4 table entries at 2PM.
Is there a way to select the grouping of the latest records without knowing how many there are?
Assuming you mean multiple entries in your Table_Logs table could have the same timestamp and you want to return each of those that were entered most recently, you need to use
GROUP BY:Field1, etc. are the fields you want to return in Table_Logs.
Here is some sample SQL Fiddle to try out.
Good luck.