title: How to get second row in PostgreSQL?
body: <p>I have next table:</p>
  
  <pre><code>COLUMN_NAME DATA_TYPE     PK    NULLABLE
  inc_id      bigint        YES   NO
  dt_cr       timestamp     NO    NO
  email       varchar(255)  NO    YES
  </code></pre>
  
  <p><code>email</code> column is not unique, so I should use <code>GROUP BY email</code>.</p>
  
  <p><strong>Question</strong>:
  How can I get from this table minimal date, next row after minimal date and email related to this dates?</p>
  
  <p>I read about <code>first_value</code> and <code>nth_value</code> functions, code listed below, but seems like it doesn't work correct.</p>
  
  <pre><code>SELECT J1.email, J2.first_date, J2.second_date 
  FROM (
      SELECT email 
      FROM orders 
      GROUP BY email) J1
  LEFT OUTER JOIN (
      SELECT email, 
          first_value(dt_cr) over (order by dt_cr ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) first_date, 
          NTH_VALUE(dt_cr, 2) over (order by dt_cr ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) second_date 
      FROM orders) J2  
  ON (J1.email=J2.email);
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>--edited
  maybe it can be done with <code>rank()</code> function..</p>