SUM of only TOP 10 rows

Cfw412 picture Cfw412 · Feb 12, 2015 · Viewed 30.6k times · Source

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

Answer

Maciej Los picture Maciej Los · Feb 12, 2015

Have you tried to use something like this:

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