How can I sum the last 15 rows in my database table?

I have a sales table with the following columns:

  • EmployeeID
  • quantity
  • date

Now I want to SUM

update the last 15 lines, so I'm currently doing:

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

      

But I get 15 lines, obviously there is a way I can sum it up and no need to iterate over and SUM it on the client side?

0


a source to share


2 answers


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

      



+10


a source


SELECT Sum(amount )
FROM
(
   SELECT Top 15 amount FROM Sales ORDER BY [Date] Desc
) as bar

      



+3


a source







All Articles