I have a table ("lms_attendance") of user registration time and time, which looks like this:
id user time io (enum) 1 9 1370931202 out 2 9 1370931664 out 3 6 1370932128 out 4 12 1370932128 out 5 12 1370933037 in
I am trying to create a view of this table that will only output the most recent record to the user ID, giving me a value of "in" or "out", so something like:
id user time io 2 9 1370931664 out 3 6 1370932128 out 5 12 1370933037 in
I'm still pretty close so far, but I realized that submissions will not accept subqueries, which makes it a lot more complicated. Nearest query:
select `lms_attendance`.`id` AS `id`, `lms_attendance`.`user` AS `user`, max(`lms_attendance`.`time`) AS `time`, `lms_attendance`.`io` AS `io` from `lms_attendance` group by `lms_attendance`.`user`, `lms_attendance`.`io`
But I get:
id user time io 3 6 1370932128 out 1 9 1370931664 out 5 12 1370933037 in 4 12 1370932128 out
It is close, but not perfect. I know that the last group should not be there, but without it it returns the most recent time, but not with a relative value of IO.
Any ideas? Thank!
sql mysql greatest-n-per-group
Keith Jun 11 '13 at 6:57 on 2013-06-11 06:57
source share