title: How to get last but one record for all customers - SQL Server 2008
body: <p>I need to get all the prior order history (last but one) of customers from the order table. The issue is there is no <code>customerId</code> or a unique value for the customers. Therefore the only way we can get the information is through customer phone number, customer address, customer name together. I wrote the below query to pull out data for a single order number but I need to know how I should use this query to pull out 10,000 customer records.Please help!</p>
  
  <pre><code>SELECT TOP 1 
      O1.*
  FROM 
      Orders AS O1
  INNER JOIN 
      Orders AS O2 ON  O1.CustAddress = O2.CustAddress
                   AND O1.CustName = O2.CustName
                   AND O1.CustPhone = O2.CustPhone
                   AND O1.OrderDate &lt; O2.OrderDate
  WHERE 
      O1.OrderId = '12345'
  ORDER BY 
      O1.OrderDate DESC
  </code></pre>