<p>In MS Transact SQL, let's say I have a table (Orders) like this:</p>

<pre><code> Order Date       Order Total     Customer #
 09/30/2008       8.00            1
 09/15/2008       6.00            1
 09/01/2008       9.50            1
 09/01/2008       1.45            2
 09/16/2008       4.50            2
 09/17/2008       8.75            3
 09/18/2008       2.50            3
</code></pre>

<p>What I need out of this is: for each customer the average order amount for the most recent two orders.  So for Customer #1, I should get 7.00 (and not 7.83).</p>

<p>I've been staring at this for an hour now (inside a larger problem, which I've solved) and I think my brain has frozen.  Help for a simple problem?</p>

This should make it
```
```
select avg(total), customer 
from orders o1 
where orderdate in 
  ( select top 2 date 
    from orders o2 
    where o2.customer = o1.customer 
    order by date desc )
group by customer
```
```