title: Oracle equivalent of Postgres' DISTINCT ON?
body: <p>In postgres, you can query for the first value of in a group with <code>DISTINCT ON</code>. <strong>How can this be achieved in Oracle?</strong></p>
  
  <p>From the postgres manual:</p>
  
  <blockquote>
    <p>SELECT DISTINCT ON ( expression [, ...] ) keeps only the first row of
    each set of rows where the given expressions evaluate to equal. The
    DISTINCT ON expressions are interpreted using the same rules as for
    ORDER BY (see above). Note that the "first row" of each set is
    unpredictable unless ORDER BY is used to ensure that the desired row
    appears first.</p>
  </blockquote>
  
  <p>For example, for a given table:</p>
  
  <pre><code> col1 | col2 
  ------+------
   A    | AB
   A    | AD
   A    | BC
   B    | AN
   B    | BA
   C    | AC
   C    | CC
  </code></pre>
  
  <p>Ascending sort: </p>
  
  <pre><code>&gt; select distinct on(col1) col1, col2 from tmp order by col1, col2 asc;
   col1 | col2 
  ------+------
   A    | AB
   B    | AN
   C    | AC
  </code></pre>
  
  <p>Descending sort: </p>
  
  <pre><code>&gt; select distinct on(col1) col1, col2 from tmp order by col1, col2 desc;
   col1 | col2 
  ------+------
   A    | BC
   B    | BA
   C    | CC
  </code></pre>