MS Sql 服务器中的累计总数

编程入门 行业动态 更新时间:2024-10-28 09:15:31
本文介绍了MS Sql 服务器中的累计总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能的重复:

Possible Duplicate: Calculate a Running Total in SqlServer

I need to get the cumulative (running) total of a column in ms-sql server. I.e. if there is a column named "Marks", then corresponding to each row cumulative sum will the sum of current and previous rows. Can we attain the result without using joins? Because my query is pretty big.

I have included a sample table and data:

CREATE TABLE "SCORE_CHART" ( "STUDENT_NAME" NVARCHAR(20), "MARKS" INT ) INSERT INTO SCORE_CHART (STUDENT_NAME, MARKS) VALUES ('STUD1', 95); INSERT INTO SCORE_CHART (STUDENT_NAME, MARKS) VALUES ('STUD2', 90); INSERT INTO SCORE_CHART (STUDENT_NAME, MARKS) VALUES ('STUD3', 98); SELECT STUDENT_NAME, MARKS FROM SCORE_CHART;

Expected result:

In oracle it’s easy to write like:

SELECT STUDENT_NAME, MARKS, SUM(MARKS) OVER (ORDER BY STUDENT_NAME) CUM_SUM FROM SCORE_CHART ORDER BY STUDENT_NAME;

Thanks in advance.

解决方案

The same query is supported from 2012 onwards. In older versions there are several approaches. Refer this www.sqlperformance/2012/07/t-sql-queries/running-totals

更多推荐

MS Sql 服务器中的累计总数

本文发布于:2023-10-14 19:13:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1491957.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:器中   总数   MS   Sql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!