title: PostgreSQL - "DISTINCT ON" and "GROUP BY" syntax
body: <p>I realized that a database query was returning unexpected results do to my improper use of "DISTINCT ON" and "GROUP BY"</p>
  
  <p>I'm hoping someone can set me straight on this. The actual query is quite complex , so I'll dumb it down :</p>
  
  <p>I have a table/inner query that consists of an object_id and a timestamp:</p>
  
  <pre><code>CREATE TABLE test_select ( object_id INT , event_timestamp timestamp );
  COPY test_select (object_id , event_timestamp) FROM stdin (DELIMITER '|');
  1           | 2013-01-27 21:01:20
  1           | 2012-06-28 14:36:26
  1           | 2013-02-21 04:16:48
  2           | 2012-06-27 19:53:05
  2           | 2013-02-03 17:35:58
  3           | 2012-06-14 20:17:00
  3           | 2013-02-15 19:03:34
  4           | 2012-06-13 13:59:47
  4           | 2013-02-23 06:31:16
  5           | 2012-07-03 01:45:56
  5           | 2012-06-11 21:33:26
  \.
  </code></pre>
  
  <p>I'm trying to select a distinct ID , ordered/deduplicated by the timestamp on reverse chron</p>
  
  <p>so the results should be [ 4, 1, 3, 2, 5 ]</p>
  
  <p>I think this does what I need (it seems to ):</p>
  
  <pre><code>SELECT object_id  
  FROM test_select 
  GROUP BY object_id 
  ORDER BY max(event_timestamp) DESC
  ;
  </code></pre>
  
  <p>For testing/auditing purposes , I sometimes want to include the timestamp field.  I can't seem to figure out how to include another field with that query.</p>
  
  <p>Can anyone point out glaring problems in my sql above, or suggestions on how to include the auditing info ?</p>