In SQL Server 2005, I would do this using a correlated subquery:

select dummy_id, date_registered, item_id, quantity, price,
       (select sum(quantity)
        from t t2
        where t2.item_id = t.item_id and
              t2.date_registered <= t.date_registered
       ) as cumulative
from table t;

If you actually want to add this into a table, you need to alter the table to add the column and then do an update. If the table has inserts and updates, you will need to add a trigger to keep it up-to-date. Getting it through a query is definitely easier.

In SQL Server 2012, you can do this using the syntax:

select dummy_id, date_registered, item_id, quantity, price,
       sum(quantity) over (partition by item_id order by date_registered) as cumulative
from table t;
Answer from Gordon Linoff on Stack Overflow
🌐
Interview Query
interviewquery.com › p › sql-cumulative-sum-guide
SQL Cumulative SUM: Window Functions, Rolling Totals & Best Practices
March 17, 2026 - SELECT customer_id, order_date, amount, SUM(amount) OVER ( PARTITION BY customer_id ORDER BY order_date ) AS cumulative_by_customer FROM orders; Here, the running total resets for each customer. This is the SQL cumulative sum group by equivalent.
Discussions

Cumulative Sum by multiple groups
It's not exactly what you're asking for, but with some fiddling around with predicates and window functions you'd be able to get the data in the order you want. Using the window function will create cumulative sums, but on a huge table it may be an expensive query. SELECT * ,SUM(Cash_Amount) OVER(PARTITION BY Location ORDER BY Sale_Date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS Location_Cumulative_Sum_Want FROM CumulativeSum ORDER BY Sale_Date More on reddit.com
🌐 r/SQL
2
7
April 10, 2019
postgresql - Grouping data based on cumulative sum - Database Administrators Stack Exchange
I have been looking online for an answer but do not really know how to formulate correctly what I would like to achieve and whether it's possible, sorry if the question sounds dumb. I am using Post... More on dba.stackexchange.com
🌐 dba.stackexchange.com
July 13, 2018
sql - How to get cumulative sum by group - Stack Overflow
Here is desired result : At ID=14, I need to reset cum value add with new cum_value. SQL : (fiddle : http://sqlfiddle.com/#!6/62f01/14) More on stackoverflow.com
🌐 stackoverflow.com
sql server - SQL Query for cumulative sum - Database Administrators Stack Exchange
Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... 1 Run SQL query with python in SQL Server raises error: SqlSatelliteCall error: Unsupported input data type in column · 0 Analytical function: sum the cumulative of previous column More on dba.stackexchange.com
🌐 dba.stackexchange.com
🌐
SQLServerCentral
sqlservercentral.com › forums › topic › cumulative-sum-by-groups
Cumulative Sum by Groups – SQLServerCentral Forums
May 22, 2023 - This reply was modified 2 years, 9 months ago by MICHALDV. ... WITH CustomerGroup AS (SELECT * FROM ( VALUES (1, 'Other Group') ,(2, 'Other Group') ,(3, 'CRNTER Group') ,(4, 'CRNTER Group') ) x(CustomerNumber, CustomerGroup) ) SELECT cg.CustomerGroup ,UserCount = COUNT (uc.Code) FROM #Customer c JOIN CustomerGroup cg ON cg.CustomerNumber = c.CustomerNumber JOIN #UserCustomer uc ON uc.CustomerNumber = c.CustomerNumber GROUP BY cg.CustomerGroup;
🌐
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
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:
🌐
Reddit
reddit.com › r/sql › cumulative sum by multiple groups
r/SQL on Reddit: Cumulative Sum by multiple groups
April 10, 2019 - The goal of /r/SQL is to provide a place for interesting and informative SQL content and discussions. ... It's not exactly what you're asking for, but with some fiddling around with predicates and window functions you'd be able to get the data in the order you want. Using the window function will create cumulative sums, but on a huge table it may be an expensive query. SELECT * ,SUM(Cash_Amount) OVER(PARTITION BY Location ORDER BY Sale_Date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS Location_Cumulative_Sum_Want FROM CumulativeSum ORDER BY Sale_Date
🌐
StrataScratch
stratascratch.com › blog › computing-cumulative-sum-in-sql-made-easy
Computing Cumulative Sum in SQL Made Easy - StrataScratch
May 8, 2025 - SELECT eu1.recorded_date, eu1.consumption, SUM(eu2.consumption) AS cumulative_consumption FROM fb_eu_energy eu1 JOIN fb_eu_energy eu2 ON eu1.recorded_date >= eu2.recorded_date GROUP BY eu1.recorded_date, eu1.consumption ORDER BY eu1.recorded_date; Tables: fb_eu_energy, fb_na_energy, fb_asia_energy ... The output returns exactly what we wanted. You can check it manually, but it really shows the cumulative consumption. A subquery in SQL is a type of query that is written inside the other query.
Find elsewhere
🌐
Oracle
blogs.oracle.com › sql › cumulative-running-total-of-previous-rows-with-sql
How to get the cumulative running total of previous rows with SQL
March 2, 2023 - So you can get cumulative sums, moving averages, etc. for each group. You can also omit order by and use partition by on its own. If you do this gives the overall total for each group. To see how partition by works, here’s another 60-second explainer: Here’s a cheat sheet recapping ways to calculate running totals in SQL...
🌐
Sqlperformance
sqlperformance.com › home › best approaches for grouped running totals
Best approaches for grouped running totals - SQLPerformance.com
June 30, 2014 - SELECT LicenseNumber, IncidentDate, TicketAmount, RunningTotal = ( SELECT SUM(TicketAmount) FROM dbo.SpeedingTickets WHERE LicenseNumber = t.LicenseNumber AND IncidentDate <= t.IncidentDate ) FROM dbo.SpeedingTickets AS t ORDER BY LicenseNumber, IncidentDate; ... SELECT t1.LicenseNumber, t1.IncidentDate, t1.TicketAmount, RunningTotal = SUM(t2.TicketAmount) FROM dbo.SpeedingTickets AS t1 INNER JOIN dbo.SpeedingTickets AS t2 ON t1.LicenseNumber = t2.LicenseNumber AND t1.IncidentDate >= t2.IncidentDate GROUP BY t1.LicenseNumber, t1.IncidentDate, t1.TicketAmount ORDER BY t1.LicenseNumber, t1.IncidentDate;
🌐
SQL WORLD
complexsql.com › home › how to get cumulative sum in sql using analytical function?
How to get cumulative sum in SQL using analytical function?
March 30, 2023 - You can calculate cumulative sum in Oracle SQL using straightforward function named Sum and order by together. If you want partitioned data then you can use partition by clause for the same.
🌐
Stack Overflow
stackoverflow.com › questions › 28805296 › how-to-get-cumulative-sum-by-group
sql - How to get cumulative sum by group - Stack Overflow
At ID value equal to 14, need to *1000) and for the next value will cumulative base on new value at ID = 14. Ex Formula in excel : IF(IDx=14,P_cum_value*1000,P_cum_value+ID) ... DECLARE @t table( id int ) INSERT INTO @t (id) VALUES (0),(1),(2),(3),(4),(5),(6), (7),(8),(9),(10),(11),(12),(13), (14),(15),(16),(17),(18),(19), (20),(21),(22),(23),(24),(99); SELECT * ,SUM(id) OVER(ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS Cum_STEPID FROM @t WHERE id < 14 UNION SELECT * ,SUM(CASE WHEN id = 14 THEN 91000 ELSE id END) OVER(ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS Cum_STEPID FROM @t WHERE id >= 14
🌐
Teradata
docs.teradata.com › r › 756LNiPSFdY~4JcCCcR5Cw › quvs_Gi2rQI8mhPkqjDwwQ
Teradata Developers Portal
June 5, 2018 - Loading application · Promo placeholder · Tracking Consent Teradata.com · Developers · Getting Started · VantageCloud Lake Documentation AI Unlimited All Documentation · Downloads · Community · Teradata Community Technical Medium Blogs Github Stack Overflow · Try for free
🌐
LearnSQL.com
learnsql.com › blog › what-is-a-running-total-and-how-to-compute-it-in-sql
What Is a Running Total and How Do You Compute It in SQL? | LearnSQL.com
Next, we’ll talk about the SQL query that builds such a sum and learn more about window functions. If you would like to compute running total in SQL, you need to be familiar with the window functions provided by your database. Window functions operate on a set of rows and return an aggregate value for each row in the result set. The syntax of the SQL window function that computes a cumulative sum across rows is:
🌐
SAS Support Communities
communities.sas.com › t5 › New-SAS-User › culmulative-sum-by-group-using-Proc-SQL › td-p › 517593
Solved: culmulative sum by group using Proc SQL - SAS Support Communities
December 4, 2018 - I don't know why you feel restricted to doing it in PROC SQL, but I have no such restrictions, so for everyone else reading along, this is a very simple method without SQL to compute these cumulative sums. proc summary data=have nway; var premiumamount sum_assured; class fiscal_year premium_date; output out=sums sum=; run; data want; set sums; by fiscal_year; if first.fiscal_year then do; cumulative_premiumamount=0; cumulative_sum_assured=0; end; cumulative_premiumamount+premiumamount; cumulative_sum_assured+sum_assured; run;
🌐
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)

🌐
Medium
medium.com › codex › dax-sql-and-r-cumulative-sums-with-groupings-686488cb6171
DAX, SQL and R — Cumulative Sums with groupings | by Peter Hui | CodeX | Medium
February 10, 2022 - CUMULATIVE_By_Level_1 = CALCULATE(SUM([Sales]), ALLEXCEPT(Sample_Table,Sample_Table[Level_1]),Sample_Table[Level_2] <= EARLIER(Sample_Table[Level_2])) This is the result. ... Explanation: Here DAX takes the grouping of Level_1 using ALLEXCEPT and compares the Level_2 value…
🌐
SQLPad
sqlpad.io › tutorial › mastering-cumulative-sums-in-sql-a-comprehensive-guide
Mastering Cumulative Sums in SQL: A Comprehensive Guide
April 29, 2024 - Basic Syntax: The basic syntax for using the OVER clause for cumulative sums is: SELECT column_name, SUM(column_name) OVER (ORDER BY column_name) AS cumulative_sum FROM table_name;
🌐
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