如何汇总数据库表中的最后15行?

时间:2021-09-30 23:00:49

I have a Sales table, with the following columns:

我有一个Sales表,其中包含以下列:

  • employeeID
  • amount
  • date

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

现在我想要汇总最后15行,所以我现在正在做:

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?

但是我明显得到了15行,有没有办法可以总结它而不必在客户端进行循环和SUM它?

2 个解决方案

#1


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

#2


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

#1


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

#2


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