title: PostgreSQL window function & difference between dates
body: <p>Suppose I have data formatted in the following way (FYI, total row count is over 30K):</p>
  
  <pre><code>customer_id             order_date  order_rank
  A                       2017-02-19  1
  A                       2017-02-24  2
  A                       2017-03-31  3
  A                       2017-07-03  4
  A                       2017-08-10  5
  B                       2016-04-24  1
  B                       2016-04-30  2
  C                       2016-07-18  1
  C                       2016-09-01  2
  C                       2016-09-13  3
  </code></pre>
  
  <p>I need a 4th column, let's call it <code>days_since_last_order</code> which, in the case where order_rank = 1 then 0 else calculate the number of days since the previous order (with rank n-1).</p>
  
  <p>So, the above would return:</p>
  
  <pre><code>customer_id             order_date  order_rank  days_since_last_order
  A                       2017-02-19  1           0
  A                       2017-02-24  2           5
  A                       2017-03-31  3           35
  A                       2017-07-03  4           94
  A                       2017-08-10  5           38
  B                       2016-04-24  1           0
  B                       2016-04-30  2           6
  C                       2016-07-18  1           79
  C                       2016-09-01  2           45
  C                       2016-09-13  3           12
  </code></pre>
  
  <p>Is there an easier way to calculate the above with a window function (or similar) rather than join the entire dataset against itself (eg. on A.order_rank = B.order_rank - 1) and doing the calc?</p>
  
  <p>Thanks!</p>