可以对特定列的前n行进行求和的SQL语句
问题描述:
我需要有关SQL语句的帮助,该语句可以求和特定列的前n行.
像这样的东西
从CALAGR中选择mySum作为TOP 3的总和(等级)(这给了我所有行的总和.我只希望总和直到第n行)
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)
答
请重做并开发您自己的查询
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
选择sum(grade)作为column_name,*来自
(请从calagr中选择前3 *)
首先选择所需的最上面的行,也可以在其中添加任何条件. select语句完成后,将其括在括号中并在其顶部应用您的函数(如此处的sum).
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).