Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server-2008
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 0: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
0 OriginalGriff 405
1 Sergey Alexandrovich Kryukov 220
2 Maciej Los 195
3 Peter Leow 190
4 DamithSL 174


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