Questions tagged [cumulative-sum]

For questions regarding implementations or algorithms for calculating cumulative sums (also known as running totals). Always add the tag for the language/platform!

A cumulative sum (also known as a running total or partial sum) refers to the concept of maintaining only a single value (the sum), which is updated each time a new value is added to the sequence.

1433 questions
190
votes
15 answers

Calculate a Running Total in SQL Server

Imagine the following table (called TestTable): id somedate somevalue -- -------- --------- 45 01/Jan/09 3 23 08/Jan/09 5 12 02/Feb/09 0 77 14/Feb/09 7 39 20/Feb/09 34 33 02/Mar/09 6 I would like a…
codeulike
  • 22,514
  • 29
  • 120
  • 167
150
votes
1 answer

Calculating Cumulative Sum in PostgreSQL

I want to find the cumulative or running amount of field and insert it from staging to table. My staging structure is something like this: ea_month id amount ea_year circle_id April 92570 1000 2014 1 April …
85
votes
9 answers

Create a Cumulative Sum Column in MySQL

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 100 2 50 150 3 10 160 Is there a…
Kirk Ouimet
  • 27,280
  • 43
  • 127
  • 177
73
votes
3 answers

Cumulative sum and percentage on column?

I have a DataFrame like this: df: fruit val1 val2 0 orange 15 3 1 apple 10 13 2 mango 5 5 How do I get Pandas to give me a cumulative sum and percentage column on only val1? Desired output: df_with_cumsum: fruit val1…
ComputerFellow
  • 11,710
  • 12
  • 50
  • 61
60
votes
6 answers

Calculate running total / running balance

I have a table: create table Transactions(Tid int,amt int) With 5 rows: insert into Transactions values(1, 100) insert into Transactions values(2, -50) insert into Transactions values(3, 100) insert into Transactions values(4, -100) insert into…
Pritesh
  • 1,938
  • 7
  • 32
  • 46
58
votes
2 answers

How can I use cumsum within a group in Pandas?

I have df = pd.DataFrame.from_dict({'id': ['A', 'B', 'A', 'C', 'D', 'B', 'C'], 'val': [1,2,-3,1,5,6,-2], 'stuff':['12','23232','13','1234','3235','3236','732323']}) id stuff val 0 A 12 1 1 B 23232 2 2 A 13 -3 3 C …
Baron Yugovich
  • 3,843
  • 12
  • 48
  • 76
33
votes
6 answers

Cumulative count of each value

I want to create a cumulative counter of the number of times each value appears. e.g. say I have the column: id 1 2 3 2 2 1 2 3 This would become: id count 1 1 2 1 3 1 2 2 2 3 1 2 2 4 3 …
user1165199
  • 6,351
  • 13
  • 44
  • 60
32
votes
14 answers

Cumulating value of current row + sum of previous rows

How would you do to transform a Column in a table from this: ColumnA ColumnB 2 a 3 b 4 c 5 d 1 a to this: ColumnA ColumnB 3 a 6(=3+3) b 10(=4+3+3) c …
Sam
  • 3,067
  • 19
  • 53
  • 55
29
votes
14 answers

List comprehension for running total

I want to get a running total from a list of numbers. For demo purposes, I start with a sequential list of numbers using range a = range(20) runningTotal = [] for n in range(len(a)): new = runningTotal[n-1] + a[n] if n > 0 else a[n] …
Kit
  • 30,365
  • 39
  • 105
  • 149
26
votes
11 answers

using LINQ to find the cumulative sum of an array of numbers in C#

I have a csv string containing doubles (e.g "0.3,0.4,0.3"), and I want to be able to output a double array containing the cumulative sum of these numbers (e.g [0.3,0.7,1.0]). So far, I have double[] probabilities = textBox_f.Text.Split(new…
simonalexander2005
  • 4,338
  • 4
  • 48
  • 92
25
votes
1 answer

SQL Server Cumulative Sum by Group

I have a table (SQL Server 2005) of this format: dummy_id, date_registered, item_id, quantity, price and I want to add a new column (cumulative) which calculates the cumulative totals of each item_id order by date_registered as shown: dummy_id …
PanosPlat
  • 940
  • 1
  • 11
  • 29
22
votes
1 answer

How to make a cumulative sequence?

Say I have a lazy sequence like the following: (def s (iterate inc 1)) (take 10 s) => (1 2 3 4 5 6 7 8 9 10) Now, I want to generate a sequence of cumulative sum of s like the following: => (1 3 6 10 15 ...) How can I do this? What I tried is to…
ntalbs
  • 28,700
  • 8
  • 66
  • 83
21
votes
6 answers

Cumulative count of unique values in R

A simplified version of my data set would look like: depth value 1 a 1 b 2 a 2 b 2 b 3 c I would like to make a new data set where, for each value of "depth", I would have the cumulative number of unique…
user2223405
  • 343
  • 1
  • 3
  • 6
19
votes
1 answer

How to calculate the cumulative sum for a vector of doubles in C++?

I have a vector of doubles and I need to create another array which is a cumulative sum of the elements of the first. For example; vector Array(10,1); vector Sum(10); Sum[0] = Array[0]; for(unsigned int i=1; i
Wawel100
  • 1,221
  • 4
  • 21
  • 26
19
votes
2 answers

MySQL cumulative sum grouped by date

I know there have been a few posts related to this, but my case is a little bit different and I wanted to get some help on this. I need to pull some data out of the database that is a cumulative count of interactions by day. currently this is what i…
John Ruddell
  • 25,283
  • 6
  • 57
  • 86
1
2 3
95 96