title: PostgreSQL - Date difference between two rows in complex query
body: <p>I've got this query:</p>
  
  <pre><code>SELECT apple, banana, update_at FROM 
  (
      SELECT DISTINCT ON (apple) *
          FROM table
              WHERE apple IN ('ad', 'sa')
  
          ORDER BY apple, update_at DESC
  ) q
  </code></pre>
  
  <p>The purpose of this query is to get the rows that has <code>apple</code> "ad" or "sa", and return the most updated row (<code>update_at (timestamp)</code>). <strong>This query works.</strong></p>
  
  <p>My problem is that I want that instead of the <code>update_at</code> column, the query will show the difference between the timestamps (duration) of the query matched row and the row which preceded it.</p>
  
  <p>Example:</p>
  
  <pre><code>apple---|banana---|update_at
  ============================
  ad------|VAL------|2017-06-01 12:12:30
  ad------|VAL------|2017-06-01 09:00:00
  ad------|VAL------|2017-05-31 13:41:00
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>apple---|banana---|update_at
  ============================
  ad------|VAL------|**03:12:30**
  </code></pre>
  
  <p>03:12:30 is the duration.</p>
  
  <p>Hope that it clear enough.</p>