title: Group by and "first" or "min"
body: <p>I have a table "messages" with the columns id,from,to,datetime,subject,body. I want to get subject and body of the latest message for each sender (from).</p>
  
  <p>Here is my query</p>
  
  <pre><code>SELECT min(subject) as tsubject,min(body) as tbody 
  FROM messages 
  WHERE "to"=28
  GROUP BY "from"
  ORDER BY "datetime"
  </code></pre>
  
  <p>This will result in </p>
  
  <pre><code>ERROR:  column "messages.datetime" must appear in the GROUP BY clause or be used in an aggregate function
  </code></pre>
  
  <p>So i get what the error is all about, but i don't want to group the result by "datetime", just the groups to be sorted so min picks the latest message from every group.</p>