title: Trying to find 2nd last order date for the customer
body: <p>I am trying to find maximum orderdate(means most recent) and the second last orderdate(means second last purchase)</p>
  
  <p>output looks like this</p>
  
  <pre><code>&gt; Emailaddress   MostRecentPurchase 2ndMostRecentPurchase   totalorder
  &gt; xyz@gmail.com  1/29/2018             1/11/2018              $30
  
  SELECT
  Customers.EmailAddress,
  '$'+CAST(SUM(Orders.PaymentAmount) AS VARCHAR(12)) as TotalOrdered
  FROM Customers,Orders
  WHERE Customers.CustomerID=Orders.CustomerID
  AND Orders.OrderDate BETWEEN '01/01/2017 00:00' AND '12/31/2017 23:59'
  AND Orders.OrderDate = ( SELECT MAX(Orders.OrderDate) 
                 FROM Orders
                 WHERE OrderDate &lt; ( SELECT MAX(OrderDate) 
                                FROM Orders
                              )
               ) 
  GROUP BY Customers.EmailAddress
  ORDER BY TotalOrdered DESC
  </code></pre>