可以对特定列的前n行进行求和的SQL语句 [英] SQL statement that can sum the top nth row of a particular column

查看:754
本文介绍了可以对特定列的前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).


这篇关于可以对特定列的前n行进行求和的SQL语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆