I have a log table, each row representing an object logging its state. Each object has a unique, unchanging GUID. There are multiple objects logging their states, so there will be thousands of entries, with objects continually inserting new logs. Everytime an object checks in, it is via an INSERT.
I have the PrimaryKey, GUID, ObjectState, and LogDate columns in tblObjects. I want to select the latest (by datetime) log entry for each unique GUID from tblObjects, in effect a ‘snapshot’ of all the objects.
How can this be accomplished?
You could use a subquery to filter for the last log entries:
Or alternatively, using not exists:
Note that the usual approach would be to store a bitflag indicating whether a row is current. That allows you to query faster.