title: How do I do a DISTINCT and ORDER BY in PostgreSQL?
body: <p>PostgreSQL is about to make me punch small animals. I'm doing the following SQL statement for MySQL to get a listing of city/state/countries that are unique.</p>
  
  <pre><code>SELECT DISTINCT city
                , state
                , country 
             FROM events 
            WHERE (city &gt; '') 
              AND (number_id = 123)  
         ORDER BY occured_at ASC
  </code></pre>
  
  <p>But doing that makes PostgreSQL throw this error:</p>
  
  <blockquote>
    <p>PGError: ERROR:  for SELECT DISTINCT, ORDER BY expressions must appear in select list</p>
  </blockquote>
  
  <p>But if I add occured_at to the SELECT, then it kills of getting back the unique list.</p>
  
  <p>Results using MySQL and first query:</p>
  
  <pre><code>BEDFORD PARK       IL   US
  ADDISON         IL  US
  HOUSTON         TX  US
  </code></pre>
  
  <p>Results if I add occured_at to the SELECT:</p>
  
  <pre><code>BEDFORD PARK       IL   US  2009-11-02 19:10:00
  BEDFORD PARK       IL   US  2009-11-02 21:40:00
  ADDISON         IL  US  2009-11-02 22:37:00
  ADDISON         IL  US  2009-11-03 00:22:00
  ADDISON         IL  US  2009-11-03 01:35:00
  HOUSTON         TX  US  2009-11-03 01:36:00
  </code></pre>
  
  <p>The first set of results is what I'm ultimately trying to get with PostgreSQL.</p>