The latest version of SQL Server (2012) permits the following.

SELECT 
    RowID, 
    Col1,
    SUM(Col1) OVER(ORDER BY RowId ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS Col2
FROM tablehh
ORDER BY RowId

or

SELECT 
    GroupID, 
    RowID, 
    Col1,
    SUM(Col1) OVER(PARTITION BY GroupID ORDER BY RowId ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS Col2
FROM tablehh
ORDER BY RowId

This is even faster. Partitioned version completes in 34 seconds over 5 million rows for me.

Thanks to Peso, who commented on the SQL Team thread referred to in another answer.

Answer from Gareth Adamson on Stack Overflow
🌐
Interview Query
interviewquery.com › p › sql-cumulative-sum-guide
SQL Cumulative SUM: Window Functions, Rolling Totals & Best Practices
March 17, 2026 - At its core, a cumulative sum is the total amount accumulated from the beginning of your dataset up to the current row. It shows how values build over time and is a common way to analyze trends in revenue, sales, or spend.
Discussions

Cumulative sum or running sum
I think for it to be a running Sum you need ORDER BY in your parenthesis. SUM(COLUMN) OVER (ORDER BY productid) More on reddit.com
🌐 r/SQL
11
5
March 10, 2023
cumulative calculation
Hi Expert, i wanted to get cumulative values as a new column in below table, --create table main2( oid int, OID2 int,SaleDateId date, quantity int,cons int) insert main1 values (1,10,'20240307',1,1), (2,10,'20240307',1,1), … More on learn.microsoft.com
🌐 learn.microsoft.com
4
0
March 3, 2022
sql - Create a Cumulative Sum Column in MySQL - Stack Overflow
I have a table that looks like this: id count 1 100 2 50 3 10 I want to add a new column called cumulative_sum, so the table would look like this: id count cumulative_sum 1 100 ... More on stackoverflow.com
🌐 stackoverflow.com
Cumulative Sum with Conditions
Here's a solution CREATE TABLE t1 ( id INT IDENTITY(1, 1), diff int ); INSERT INTO t1(diff) VALUES (100); insert into t1(diff) values (300); insert into t1(diff) values (-500); insert into t1(diff) values (100); insert into t1(diff) values (300); declare @ret int; select @ret =0; SELECT t1.id, t1.diff, 0 as sum into t2 from t1; update t2 set @ret = sum = case when @ret + diff <0 then 0 else @ret + diff end select * from t2 Sql fiddle to play with: https://sqlfiddle.com/sql-server/online-compiler?id=4640259d-77c0-4fb3-a0cb-e88a8ad41529 More on reddit.com
🌐 r/SQL
7
5
February 21, 2025
🌐
Dawiso
dawiso.com › home › glossary › sql sum() over - guide to running totals and cumulative sums
SQL SUM() OVER - Guide to Running Totals and Cumulative Sums | Dawiso
March 20, 2026 - ... SUM() OVER adds a cumulative or windowed sum column to your result set. With ORDER BY, it computes a running total. With PARTITION BY, it restarts the running total for each group.
🌐
StrataScratch
stratascratch.com › blog › computing-cumulative-sum-in-sql-made-easy
Computing Cumulative Sum in SQL Made Easy - StrataScratch
May 8, 2025 - A cumulative sum is calculated by adding all previous values in a sequence to the current value. The sequence is usually a date or time, so the cumulative sum gives you a sum at a certain time.
🌐
SQLPad
sqlpad.io › tutorial › mastering-cumulative-sums-in-sql-a-comprehensive-guide
Mastering Cumulative Sums in SQL: A Comprehensive Guide
April 29, 2024 - SELECT employee_id, region, SUM(performance_score) OVER (PARTITION BY region ORDER BY employee_id) AS cumulative_performance FROM employee_performance; This advanced use of the OVER clause not only aids in detailed performance analysis but also in making informed decisions on training and development needs across different geographical locations. Window functions in SQL are a powerhouse for data analysis, offering unparalleled capabilities for computing cumulative sums.
🌐
Five
five.co › blog › sql-cumulative-sum
SQL Cumulative Sum: A Practical Guide | Five
May 4, 2026 - A cumulative sum, also known as a running total, is the sum of a sequence of numbers which is updated each time a new number is added to the sequence. In SQL, this translates to calculating the sum of a column’s values up to the current row.
🌐
Reddit
reddit.com › r/sql › cumulative sum or running sum
r/SQL on Reddit: Cumulative sum or running sum
March 10, 2023 -

Hello!

I am trying to write a SQL query using a combination of CTE and windows function to calculate a cumulative sum of the count of product.

My apologize as I'm using my mobile but here is an example of the dataset

Productcode RATE1 RATE1 RATE3 RATE1 RATE3 RATE2

The original table has a lot of columns but I'm only interested into 1. It also has customer id but a customer can only have 1 product. There is also date row but I'm not interested in the date

I wrote the below with the idea of taking a column out of many from the original dataset, group the product code and get individual count of each rate and finally add a column running a cumulative sum

WITH testtable AS (Select Productcode ,Count(*) as Freq From table group by 1) Select Productcode ,Sum(Freq) over (partition by productcode) as cumul From testtable

The output is essentially a duplicate of the column Freq as opposed to the cumulative Freq as I hoped.

I also appreciate that there is certainly an easier way of getting this done but I still would like to know why it is not working?

Thanks for your inputs!

Edit: moved 'AS' to the correct position Edit 2: sample dataset and further clarifications Edit 3: solved SUM(COLUMN) OVER (ORDER BY productid)

Find elsewhere
🌐
Microsoft Learn
learn.microsoft.com › en-us › sql › t-sql › functions › sum-transact-sql
SUM (Transact-SQL) - SQL Server | Microsoft Learn
July 20, 2026 - SELECT BusinessEntityID, TerritoryID, DATEPART(yy, ModifiedDate) AS SalesYear, CONVERT (VARCHAR (20), SalesYTD, 1) AS SalesYTD, CONVERT (VARCHAR (20), AVG(SalesYTD) OVER (PARTITION BY TerritoryID ORDER BY DATEPART(yy, ModifiedDate)), 1) AS MovingAvg, CONVERT (VARCHAR (20), SUM(SalesYTD) OVER (PARTITION BY TerritoryID ORDER BY DATEPART(yy, ModifiedDate)), 1) AS CumulativeTotal FROM Sales.SalesPerson WHERE TerritoryID IS NULL OR TerritoryID < 5 ORDER BY TerritoryID, SalesYear; Here's the result set. BusinessEntityID TerritoryID SalesYear SalesYTD MovingAvg CumulativeTotal ---------------- ------
🌐
PopSQL
popsql.com › learn-sql › sql-server › how-to-calculate-cumulative-sum-running-total-in-sql-server
SQL Server: Cumulative Sum/Running Total by Day or Group - PopSQL
Let's say we want to see a hockey stick graph of our cumulative sessions by day in SQL Server. First, we'll need a table with a day column and a count column: select convert(varchar(10), start_date, 105) as day, count(1) from sessions group by convert(varchar(10), start_date, 105); day | count ------------+------- 02-02-2020 | 3 03-02-2020 | 3 04-02-2020 | 4 · Next, we'll write a SQL Server common table expression (CTE) and use a window function to keep track of the cumulative sum/running total:
🌐
Medium
medium.com › geekculture › sql-interview-question6-finding-cumulative-sum-use-case-of-sum-over-aggregate-window-dfe379d39f63
SQL Interview Question6: Finding Cumulative Sum — Use Case of SUM() OVER () Aggregate Window Function | by Deeksha Singh | Geek Culture | Medium
December 1, 2022 - Here using sum() function with over() clause as window function will do our job i.e., it won’t aggregate the output in one row. Let’s do that not mentioning anything in over() clause, ... Since, we have not mentioned anything in over() clause, it is aggregating total salaries of 120 records and showing it along with the individual salaries. But we need cumulative sum, so have to partition it by employees and arrange it in ascending order of year in order by clause.
🌐
Quora
quora.com › How-do-you-do-a-cumulative-sum-in-SQL
How to do a cumulative sum in SQL - Quora
Answer (1 of 4): It’s pretty straightforward with the [code ]lag()[/code] window function. For instance, in SQLite: [code].mode table .print ==> create and populate a sample table with a Fibonacci-type series CREATE TABLE a(b REAL); INSERT INTO a VALUES (0.1), (0.2), (0.3), (0.5), (0.8), ...
🌐
GeeksforGeeks
geeksforgeeks.org › sql › calculate-running-total-in-sql
Calculate Running Total in SQL - GeeksforGeeks
November 17, 2025 - Query to Calculate Running Total in SQL Server · SELECT * ,SUM([SALARY]) OVER ( ORDER BY [ID] ) AS [Running Total] FROM department
🌐
Bipp
bipp.io › sql-tutorial › ms-sql › calculate-cumulative-total
How to Calculate Cumulative Sum-Running Total | Analysis | MS-SQL | bipp Analytics
Let’s say we want to see a report with cumulative values, for example the cumulative daily revenue at different timestamps. We want the cumulative revenue at each timestamp in the table: select sales_ts, sum(amount) over (order by sales_ts) as cumulative from sales
🌐
Coffingdw
coffingdw.com › sql-server-analytics-cumulative-sum
SQL Server Analytics – Cumulative Sum – Software connecting all databases
A cumulative sum is a running total of values progressively increasing or accumulating over time or across a dataset. It represents the summation of all values encountered up to a certain point or row in the dataset, where each new value adds to the total of the previous values.
🌐
LinkedIn
linkedin.com › pulse › using-window-functions-get-cumulative-sum-sql-josé-erildo-1f
Using Window Functions to Get Cumulative Sum in SQL
September 29, 2023 - Furthermore, in the Over clause we will need to specify ROWS BETWEEN UNBOUNDEDPRECEDING AND CURRENT ROW. ... | date | amount ------------------ 2023-06-01 | 926 2023-06-02 | 774 2023-06-03 | 552 2023-06-04 | 858 2023-06-05 | 753 2023-06-06 | 881 2023-07-01 | 501 2023-07-06 | 644 2023-07-07 | 615 2023-07-08 | 773 2023-07-09 | 555 ... select date, amount, sum(amount) over(order by date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) as cumulativesum from table;
🌐
1Keydata
1keydata.com › sql › advanced sql › running totals
SQL Running Totals | Calculate Cumulative Sums in SQL
A regular SUM aggregates all rows into one value. A running total keeps each row separate and adds a cumulative column that progressively accumulates — each row shows the sum of all rows up to and including that point.
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 758674 › cumulative-calculation
cumulative calculation - Microsoft Q&A
March 3, 2022 - You first need to number the rows in ascending order to get a row number and then divide quantity/cons by this number, and then compute a running sum in reverse order from this. ... WITH numbering AS ( SELECT oid, OID2, SaleDateId, quantity, ...
🌐
Medium
medium.com › @mikyung.jin.94 › sql-mastering-cumulative-sum-aa1867e7ee9b
SQL — Mastering Cumulative Sum()
November 29, 2023 - In this case, SQL statements implements the query based on OVER() clause.