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.

1097 questions
174
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
  • 20,946
  • 27
  • 113
  • 161
97
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 …
74
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
  • 23,368
  • 41
  • 108
  • 164
63
votes
2 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
  • 9,536
  • 10
  • 44
  • 59
58
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,810
  • 7
  • 29
  • 43
32
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
  • 5,057
  • 12
  • 40
  • 57
26
votes
13 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
  • 26,307
  • 30
  • 94
  • 145
21
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
  • 3,672
  • 4
  • 39
  • 74
21
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
  • 24,984
  • 7
  • 57
  • 76
20
votes
10 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
  • 2,669
  • 18
  • 48
  • 55
18
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
  • 263
  • 3
  • 5
17
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
  • 760
  • 1
  • 7
  • 25
16
votes
3 answers

Discounted Cumulative Sum in R

I'm trying to calculate a discounted cumulative sum in which the later values are worth more. Let's say I have the following dataset: dt <- data.table( "year" = c(79,80,81,82,83), "value" = c(5,2,6,8,9)) > dt year value 1: 79 5 2: 80 …
lovestacksflow
  • 507
  • 2
  • 13
16
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,031
  • 3
  • 18
  • 26
15
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…
John Ruddell
  • 22,365
  • 5
  • 46
  • 79
1
2 3
73 74