Click here to Skip to main content
14,599,610 members
Rate this:
Please Sign up or sign in to vote.
I want help with SQL statement that can sum the top nth row of a particular column.
something like this
SELECT TOP 3 SUM(GRADE) AS mySUM FROM CALAGR(this gives me the sum of all the rows.I want the sum only up to the the nth row)
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

REfere this and develope your own query

SELECT sum(salary)
FROM (SELECT ROW_NUMBER() OVER (ORDER BY empid) AS Row, empid,salary
     FROM Emp ) us
WHERE Row <4
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

select sum(grade) as column_name,* from
(select top 3 * from calagr)

first select the top rows you want to, you can add any condition in it too. after your select statement is complete, enclose it in parentheses and at the top of it apply your function (like sum here).
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100