Skip to content
Advertisement

Select row with most recent date per user

I have a table (“lms_attendance”) of users’ check-in and out times that looks like this:

I’m trying to create a view of this table that would output only the most recent record per user id, while giving me the “in” or “out” value, so something like:

I’m pretty close so far, but I realized that views won’t accept subquerys, which is making it a lot harder. The closest query I got was :

But what I get is :

Which is close, but not perfect. I know that last group by shouldn’t be there, but without it, it returns the most recent time, but not with it’s relative IO value.

Any ideas? Thanks!

Advertisement

Answer

Query:

SQLFIDDLEExample

Result:

Note that if a user has multiple records with the same “maximum” time, the query above will return more than one record. If you only want 1 record per user, use the query below:

SQLFIDDLEExample

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement