SQL 仅前 10 行的总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28479926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:17:11  来源:igfitidea点击:

SUM of only TOP 10 rows

sqlsumtop-n

提问by Cfw412

I have a query where I am only selecting the TOP 10 rows, but I have a SUM function in there that is still taking the sum of all the rows (disregarding the TOP 10). How do I get the total of only the top 10 rows? Here is my SUM function :

我有一个查询,我只选择前 10 行,但我在那里有一个 SUM 函数,它仍在计算所有行的总和(不考虑前 10 行)。如何仅获得前 10 行的总数?这是我的 SUM 函数:

SUM( fact.Purchase_Total_Amount) Total

回答by Maciej Los

Have you tried to use something like this:

你有没有试过使用这样的东西:

SELECT SUM(Whatever)
FROM (
    SELECT TOP(10) Whatever
    FROM TableName
) AS T

回答by LCIII

Use the TOP feature with a nested query

将 TOP 功能与嵌套查询一起使用

SELECT SUM(innerTable.Purchase_Total_Amount) FROM
(SELECT TOP 10 Purchase_Total_Amount FROM Table) as innerTable

回答by lj9799

SELECT SUM(Whatever)
FROM (
    SELECT TOP(10) Whatever
    FROM TableName
) AS T