SUM of only TOP 10 rows

柔情痞子 提交于 2020-12-01 09:53:52

问题


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 :

SUM( fact.Purchase_Total_Amount) Total

回答1:


Have you tried to use something like this:

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



回答2:


Use the TOP feature with a nested query

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


来源:https://stackoverflow.com/questions/28479926/sum-of-only-top-10-rows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!