Click here to Skip to main content
12,757,627 members (36,778 online)
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 6-Oct-12 1:12am
Rate this: bad
 
good
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
  Permalink  
Rate this: bad
 
good
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).
  Permalink  

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

    Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 6 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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