sql server 2005 - SQL sub-query problem with grouping, average -


in ms transact sql, let's have table (orders) this:

 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 

what need out of is: each customer average order amount recent 2 orders. customer #1, should 7.00 (and not 7.83).

i've been staring @ hour (inside larger problem, i've solved) , think brain has frozen. simple problem?

this should make it

select avg(total), customer  orders o1  orderdate in    ( select top 2 date      orders o2      o2.customer = o1.customer      order date desc ) group customer 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -