title: Query to pull previous order date corresponding to a customer?
body: <p>I have a schema with Customer table and Order table. A customer can place order in multiple dates. I need to have previous order_date for every order_date corresponding to a customer.</p>
  
  <p>Say a customer placed 4 orders, then for newest order(4th order) - it must pull current order_date and previous order_date(3rd order). For 3rd order placed by customer, it must pull 3rd order_date as current order_date and previous order_date(2nd order) as so on.</p>
  
  <p>I am using below query to get previous order_date and then joining with current_query to get result::</p>
  
  <pre><code>select customerid, orderid, order_date as previous_order_date
  from (
      select c.customerid, o.orderid, o.order_date, 
          row_number() over (partition by c.customerid, o.orderid
                             order by o.order_date) rown
      from customers c join orders o on c.customerid = o.customerid
         ) a
  where rown = 2
  </code></pre>
  
  <p>But the issue is, I am getting a single date corresponding to a customerid whereas the requirement is - just previous order_date corresponding to current order_date for a customer.</p>
  
  <p>Any suggestion would help! Thanks</p>