Take a look to window (or analytic) functions. Unlike aggregate functions, window functions preserve resulting rows and facilitate operations related to them. When using order by in over clause, windowing is done from first row to current row according to specified order, which is exactly what you need.

select year, week, sum(number_of_records) over (order by year, week)
from (
  select year(creation_date) as year, weekofyear(creation_date) as week,
  count(id) as number_of_records
  from input group by year, week
) your_sql

I guess you will also need to reset sum for each year, which I leave as exercise for you (hint: partition clause).

Answer from Tomáš Záluský 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 order_date, amount, SUM(amount) OVER ( ORDER BY order_date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW ) AS rolling_7day FROM orders; This calculates a SQL rolling sum last 7 days, letting you analyze short-term performance rather than an ...
Discussions

sql - Cumulative total count after each week passes for a given date range - Stack Overflow
I have a dataset that indicates whether medical patients received a certain procedure on their visit date within the year 2020. It looks something like this: PatientID Visit Date Procedure Ind 12... More on stackoverflow.com
🌐 stackoverflow.com
sql server 2008 r2 - Cumulative Sum of week sales - Stack Overflow
SELECT WeeklyReportDetailsDataId, ItemId, StoreId, ManufacturerRetailerAssocId, WeekSales, WKYR, SUM(WeekSales) OVER (PARTITION BY ItemId, StoreId, ManufacturerRetailerAssocId ... More on stackoverflow.com
🌐 stackoverflow.com
join - Cumulative total by week - postgresql - Stack Overflow
Using postgresql, version 9.5.8 Below I've got a working query, which gives me the pct of ready accounts of all accounts. This table is then split by week, giving me the amount of accounts created... More on stackoverflow.com
🌐 stackoverflow.com
September 2, 2017
mysql - Rolling count of total transactions over time - Database Administrators Stack Exchange
I need to get a set of total transactions over time on a weekly interval. Essentially I need a total-transactions-to-date column. When I group by WEEK(Date) I get the amount of transactions for tha... More on dba.stackexchange.com
🌐 dba.stackexchange.com
August 21, 2014
🌐
SitePoint
sitepoint.com › databases
Cumulative SUM per day, week and month - Databases - SitePoint Forums | Web Development & Design Community
September 21, 2014 - I need to fetch the cumulative sum for sales made by a marketeer per day, week and month. With the day I don’t have any problems running the following query: SELECT SUM(sales_made) FROM marketeer_sales WHERE markete…
🌐
Count
count.co › sql-resources › snowflake › running-totals
Running Totals | Snowflake - Count.co
October 20, 2025 - SUM(column) OVER (PARTITION BY col_to_group_by, ORDER BY column_to_order_by) expr1 This is an expression that evaluates to a numeric data type (INTEGER, FLOAT, DECIMAL, etc.). expr2 This is the optional expression to partition by. expr3 This is the optional expression to order by within each partition. (This does not control the order of the entire query output.) ... SELECT DATE_TRUNC('WEEK',START_TIME) WEEK, DATE_PART('WEEKDAY',START_TIME) DAY, SUM(DURATION)/(60*60) DAILY_HOURS_WATCHED FROM PUBLIC.NETFLIX WHERE WEEK = '2018-11-26' GROUP BY WEEK,DAY ORDER BY WEEK,DAY
🌐
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
🌐
CodingSight
codingsight.com › home › calculating running total with over clause and partition by clause in sql server
Calculating Running Total with OVER Clause and PARTITION BY Clause in SQL Server
October 13, 2022 - The article provides an example and possible issues of calculating a running total using the OVER clause, partitioning a running total by column values in SQL Server.
🌐
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:
Find elsewhere
🌐
Wagonhq
wagonhq.com › blog › running-totals-sql
Calculating Running Totals using SQL - Wagon
How do we generate the following table of cumulative sales over time? In SQL, there are two typical approaches: a self join or a window function. A self join is a query that compares a table to itself. In this case, we’re comparing each date to any date less than or equal to it in order to calculate the running total. Concretely, we take the sum of sales in the second table over every row that has a date less than or equal to the date coming from the first table.
🌐
Stack Overflow
stackoverflow.com › questions › 73572721 › cumulative-total-count-after-each-week-passes-for-a-given-date-range
sql - Cumulative total count after each week passes for a given date range - Stack Overflow
Then, as of the second week of the year (1/8/2020 - 1/14/2020), y number of cumulative patients have had the procedure (so first week + second week), and so on. It would look something like this: I've been trying to do this for a couple of days but have hit a wall. Any help would be appreciated, thank you!! ... SELECT t.date, MAX(t.commulative_count) as commulative_count FROM ( SELECT Concat(DATEADD(DAY,1-DATEPART(WEEK, Visit_date),Visit_date), '-' ,DATEADD(DAY,7-DATEPART(WEEK, Visit_date),Visit_date)) as date, SUM(Procedure_Ind) OVER (ORDER BY Visit_date) as commulative_count FROM tableA )t GROUP BY t.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.
🌐
Stack Overflow
stackoverflow.com › questions › 23344479 › cumulative-sum-of-week-sales
sql server 2008 r2 - Cumulative Sum of week sales - Stack Overflow
SELECT WeeklyReportDetailsDataId, ItemId, StoreId, ManufacturerRetailerAssocId, WeekSales, WKYR, SUM(WeekSales) OVER (PARTITION BY ItemId, StoreId, ManufacturerRetailerAssocId ORDER BY ItemId, StoreId, ManufacturerRetailerAssocId ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROWS) as ytd FROM WeeklyReportDetailsData WHERE ManufacturerRetailerAssocId = 10 GROUP BY ItemId, StoreId, WeekSales, WeeklyReportDetailsDataId, WKYR, ManufacturerRetailerAssocId ORDER BY WeeklyReportDetailsDataId, ItemId, StoreId, ManufacturerRetailerAssocId, WKYR
🌐
Enterprise DNA
blog.enterprisedna.co › running-total-sql
Enterprise DNA: We Help Businesses Put Data and AI to Work
Short, practical notes on what actually works with AI right now, straight from our founder. New posts most weeks.
🌐
Essential SQL
essentialsql.com › home › use sql to calculate a running total
Use SQL to Calculate a Running Total - Essential SQL
March 4, 2023 - There are several ways to calculate a running total in SQL. In this article, we will cover two methods: Joins, and Window Functions.
🌐
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 - Learn how to use window functions to calculate running totals such as cumulative sums and moving averages with SQL.
🌐
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.
🌐
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:
🌐
Stack Overflow
stackoverflow.com › questions › 46013207 › cumulative-total-by-week-postgresql
join - Cumulative total by week - postgresql - Stack Overflow
September 2, 2017 - Use your query without the last column in a derived table (a subquery in FROM clause) and use sum() as a window function. Calculate the percentages in an outer wrapper query: select week_created, total_accounts, accounts_ready, concat((acco...
Top answer
1 of 3
2

What you want is called the cumulative sum, you can do something like:

create table transactions (transactionid int, d date);
insert into transactions (transactionid, d) 
    values (1, '2014-08-04'),(2,'2014-08-05'), (3, '2014-08-18')
         , (4, '2014-08-18'), (5,'2014-08-20');

select x.y, x.w,  count(1) 
from ( 
   select distinct year(d) as y, week(d) as w 
   from transactions
) as x 
join transactions y 
    on year(y.d) < x.y
    or ( year(y.d) = x.y
     and week(y.d) <= x.w ) 
group by x.y, x.w;  

+------+------+----------+
| y    | w    | count(1) |
+------+------+----------+
| 2014 |   31 |        2 |
| 2014 |   33 |        5 |
+------+------+----------+

I did not see your additional request for 2 2 for 2014. You can do that by replacing:

select distinct year(d) as y, week(d) as w 
from transactions 

...with an expression that creates the whole domain for weeks. It is often a good idea to create a calendar table that you can use to join against to get reports for missing values etc.

2 of 3
2

To get the basic data you need an aggregation:

select 1 + floor(datediff(date, mind) / 7) as week,
       year(date) as year,
       count(*) as num
from atable t cross join
     (select min(date) as mind
      from atable
     ) td
group by 1 + floor(datediff(date, mind) / 7),
         year(date)

You can extend this using variables to get the cumulative sum:

select week, year, num, (@cum := @cum + num) as cum
from (select 1 + floor(datediff(date, mind) / 7) as week,
             year(date) as year,
             count(*) as num
      from atable t cross join
           (select min(date) as mind
            from atable
           ) td
      group by 1 + floor(datediff(date, mind) / 7),
               year(date)
     ) x cross join
     (select @cum := 0) vars
order by year, week;
🌐
Medium
eisultan.medium.com › sql-running-totals-window-functions-8d8011ba6007
SQL Running Totals & Window Functions | by Noah Sultan, PhD | Medium
February 16, 2024 - SELECT date, sales, SUM(sales) OVER (ORDER BY date RANGE BETWEEN INTERVAL 7 DAYS PRECEDING AND CURRENT ROW) AS weekly_total FROM Registration_data; The same method can be used to compute running total of revenue over any time range, or even ...