I've got a table of visits(person, visit_type, date, ...), I would like to group by visit_type and person and then select the record in each group that has the MAX(date). I'm pretty sure it should be simple using query algebra rather than writing SQL, but can't seem to get it to work. I thought it would be something like:
Visit.group(:visit_type, :person).having("date = MAX(date)")
But that does not choose the single record from each group.
EDIT: Ahhh! after a bit more investigation - it turns out the above does work! Yay!
EDIT: Ahhh! It works on sqlite and mySql, but not on postgres, something to do with stricter implementation of the group_by function according to the standard. Boo, rubbish! This means I'm still looking for an answer.
EDIT: I do not think this gives correct results every time on sqlite.