views:

168

answers:

2

I have a Sales table, with the following columns:

  • employeeID
  • amount
  • date

Now I want to SUM up the last 15 rows, so I am currently doing:

SELECT TOP 15 SUM(amount) FROM Sales ORDER BY [Date] DESC

But I get 15 rows obviously, is there a way I can sum it up and not have to loop through and SUM it on the client side?

+10  A: 
SELECT
    SUM (Amount)
FROM
    (SELECT TOP 15 amount FROM Sales ORDER BY [Date] DESC) foo
gbn
+3  A: 
SELECT Sum(amount )
FROM
(
   SELECT Top 15 amount FROM Sales ORDER BY [Date] Desc
) as bar
Eoin Campbell
should I use foo or bar?
Blankman
YES ! DEFINITELY !
marc_s
Split the difference, compromise and use "foobar"...
gbn
lol... good work gbn... "great minds" and all that ;)
Eoin Campbell