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
17
votes
3 answers

cumulative plot using ggplot2

I'm learning to use ggplot2 and am looking for the smallest ggplot2 code that reproduces the base::plot result below. I've tried a few things and they all ended up being horrendously long, so I'm looking for the smallest expression and ideally would…
eddi
  • 49,088
  • 6
  • 104
  • 155
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
  • 521
  • 3
  • 14
16
votes
2 answers

Pandas Efficient VWAP Calculation

I have the below code, using which I can calculate the volume-weighted average price by three lines of Pandas code. import numpy as np import pandas as pd from pandas.io.data import DataReader import datetime as dt df = DataReader(['AAPL'],…
Zhubarb
  • 11,432
  • 18
  • 75
  • 114
14
votes
2 answers

How to calculate the running total using aggregate?

I'm developing a simple financial app for keeping track of incomes and outcomes. For the sake of simplicity, let's suppose these are some of my documents: { description: "test1", amount: 100, dateEntry: ISODate("2015-01-07T23:00:00Z") } {…
Fabio B.
  • 9,138
  • 25
  • 105
  • 177
13
votes
3 answers

Running total by grouped records in table

I have a table like this (Oracle, 10) Account Bookdate Amount 1 20080101 100 1 20080102 101 2 20080102 200 1 20080103 -200 ... What I need is new table grouped by Account…
Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
13
votes
3 answers

SQLite: accumulator (sum) column in a SELECT statement

I have a table like this one: SELECT value FROM table; value 1 3 13 1 5 I would like to add an accumulator column, so that I have this result: value accumulated 1 1 3 4 13 17 1 18 5 23 How can I do this? What's the real…
moala
  • 5,094
  • 9
  • 45
  • 66
13
votes
4 answers

Creating a running counting variable in R?

I have a dataset of soccer match results, and I am hoping to learn R by creating a running set of ratings similar to the World Football Elo formula. I am running into trouble with things that seem to be simple in Excel aren't exactly intuitive in…
Matt Barger
  • 173
  • 1
  • 6
13
votes
4 answers

Oracle SQL Analytic query - recursive spreadsheet-like running total

I have the following data, composed of the A value, ordered by MM (month). The B column is computed as GREATEST(current value of A + previous value of B, 0) in a spreadsheet-like fashion. How can I compute B using a SQL Query? I tried using…
12
votes
3 answers

Running Total by Group SQL (Oracle)

I have a table in an Oracle db that has the following fields of interest: Location, Product, Date, Amount. I would like to write a query that would get a running total of amount by Location, Product, and Date. I put an example table below of what…
user1723699
  • 1,031
  • 6
  • 13
  • 27
12
votes
4 answers

MongoDB Aggregation: Compute Running Totals from sum of previous rows

Sample Documents: { time: ISODate("2013-10-10T20:55:36Z"), value: 1 } { time: ISODate("2013-10-10T22:43:16Z"), value: 2 } { time: ISODate("2013-10-11T19:12:66Z"), value: 3 } { time: ISODate("2013-10-11T10:15:38Z"), value: 4 } { time:…
12
votes
7 answers

Calculate cumulative average (mean)

I would like to know how to calculate the cumulative average for some numbers. I will give a simple example to describe what I am looking for. I have the following numbers vec <- c(1, 2, 3, 4, 5) If I do the average of these numbers I will get 3…
MR BIG
  • 163
  • 1
  • 1
  • 6
10
votes
1 answer

Cumulative sum in Spark

I want to do cumulative sum in Spark. Here is the register table (input): +---------------+-------------------+----+----+----+ | product_id| date_time|…
lucy
  • 4,136
  • 5
  • 30
  • 47
10
votes
2 answers

Reset Running Total based on another column

Am trying to calculate running total. But it should reset when the cummulative sum greater than another column value create table #reset_runn_total ( id int identity(1,1), val int, reset_val int ) insert into #reset_runn_total values…
Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
10
votes
1 answer

How to SUM() each row into another column

I have this table | ID_prim | ID (FKey) | Date | Moved Items | |:-----------|:------------|-------------:|:------------:| | 1003 | 12_1 | nov 2013 | 2 | | 1003 | 12_2 | okt 2013 | 3 …
Igoranze
  • 1,506
  • 13
  • 35
10
votes
3 answers

Cumulative sum over index in MATLAB

Consider the following matrix, where the first column is the index, the second - is values, the third - is the cumulative sum which resets once the index changes: 1 1 1 % 1 1 2 3 % 1+2 1 3 6 % 3+3 2 4 4…
Thorbjörn
  • 125
  • 6
1
2
3
95 96