title: rails activerecord arel group_by something and selecting a max(col) record for each group
body: <p>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:</p>
  
  <pre><code>Visit.group(:visit_type, :person).having("date = MAX(date)")
  </code></pre>
  
  <p>But that does not choose the single record from each group.</p>
  
  <p>EDIT: Ahhh! after a bit more investigation - it turns out the above does work! Yay!</p>
  
  <p>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.</p>
  
  <p>EDIT: I do not think this gives correct results every time on sqlite.</p>