title: Oracle ranking columns on multiple fields
body: <p>I am having some issues with ranking some columns in Oracle. I have two columns I need to rank--a group id and a date.</p>
  
  <p>I want to group the table two ways:</p>
  
  <ol>
  <li>Rank the records in each GROUP_ID by DATETIME (RANK_1)</li>
  <li>Rank the GROUP_IDs by their DATETIME, GROUP_ID (RANK_2)</li>
  </ol>
  
  <p>It should look like this:</p>
  
  <pre><code>GROUP_ID  |    DATE    |   RANK_1  |  RANK_2
  ----------|------------|-----------|----------
      2     |  1/1/2012  |      1    |    1
      2     |  1/2/2012  |      2    |    1
      2     |  1/4/2012  |      3    |    1   
      3     |  1/1/2012  |      1    |    2
      1     |  1/3/2012  |      1    |    3
  </code></pre>
  
  <p>I have been able to do the former, but have been unable to figure out the latter. </p>
  
  <pre><code>SELECT   group_id,
           datetime,
           ROW_NUMBER() OVER (PARTITION BY group_id ORDER BY datetime) AS rn,
           DENSE_RANK() OVER (ORDER BY group_id) AS rn2
  FROM     table_1
  ORDER BY group_id;
  </code></pre>
  
  <p>This incorrectly orders the RANK_2 field:</p>
  
  <pre><code>GROUP_ID  |    DATE    |   RANK_1  |  RANK_2
  ----------|------------|-----------|----------
      1     |  1/3/2012  |     1     |    1
      2     |  1/1/2012  |     1     |    2
      2     |  1/2/2012  |     2     |    2
      2     |  1/4/2012  |     3     |    2
      3     |  1/1/2012  |     1     |    3
  </code></pre>