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
10
votes
5 answers

how to cumulatively add values in one vector in R

I have a data set that looks like this id name year job job2 1 Jane 1980 Worker 0 1 Jane 1981 Manager 1 1 Jane 1982 Manager 1 1 Jane 1983 Manager 1 1 Jane 1984 Manager 1 1 Jane 1985 Manager…
halo09876
  • 2,725
  • 12
  • 51
  • 71
10
votes
3 answers

Cumulative sum over a set of rows in mysql

I have a complex query(containing multiple joins, unions) that returns a set of rows containing id, day, hr, amount. The output of the query looks like this: id day hr amount 1 1 1 10 1 1 2 25 1 1 3 30 …
user1051577
  • 153
  • 1
  • 1
  • 7
9
votes
5 answers

Group numeric vector by predefined maximal group sum

I have a numeric vector like this x <- c(1, 23, 7, 10, 9, 2, 4) and I want to group the elements from left to right with the constrain that each group sum must not exceed 25. Thus, here the first group is c(1, 23), the second is c(7, 10) and…
LulY
  • 976
  • 1
  • 9
  • 24
9
votes
1 answer

SQL Server : running Sum() Over Partition

I am working on a MS Access frontend with a SQL Server 2017 as backend storing tables and views. I want to calculate (on the SQL Server side) the quantity of stock (Bestand), remaining stock (Restbestand) and reserved stock (Reserviert) from…
Java-Jim
  • 93
  • 5
9
votes
2 answers

Pyspark : Cumulative Sum with reset condition

We have dataframe like below : +------+--------------------+ | Flag | value| +------+--------------------+ |1 |5 | |1 |4 | |1 |3 | |1 |5 | |1 …
swapnil
  • 91
  • 1
  • 3
8
votes
3 answers

Calculate balance with mysql

I have a table which contains the following data: ID In Out 1 100.00 0.00 2 10.00 0.00 3 0.00 70.00 4 5.00 0.00 5 0.00 60.00 6 20.00 0.00 Now I need a query…
Mannitou
  • 105
  • 1
  • 1
  • 4
8
votes
2 answers

Optimizing a Vertica SQL query to do running totals

I have a table S with time series data like this: key day delta For a given key, it's possible but unlikely that days will be missing. I'd like to construct a cumulative column from the delta values (positive INTs), for the purposes of…
user879681
  • 81
  • 1
  • 3
8
votes
2 answers

Fast algorithm for sum of steps taken by the Euclidean algorithm over pairs of numbers under an upper bound

Note: This may involve a good deal of number theory, but the formula I found online is only an approximation, so I believe an exact solution requires some sort of iterative calculation by a computer. My goal is to find an efficient algorithm (in…
8
votes
4 answers

Array's cumulative sum

I have an integer array of values and want to find a simple way of calculating its cumulative sum (S = Data(1) + Data(2) + ... + Data(x)). I already created this function: Function CumulativeSum(Data() As Integer, k As Integer) As Integer For…
Pspl
  • 1,398
  • 12
  • 23
8
votes
1 answer

How to calculate cumulative sum?

I have data containing columns biweek and Total, I want to get cumulative sum on biweek basis. My data is like: biweek Total 0 3060.913 1 4394.163 2 3413.748 3 2917.548 4 3442.055 5 3348.398 6 1771.722 and I want to get output like…
user6559913
  • 483
  • 4
  • 7
  • 15
8
votes
3 answers

R cumulative sum by condition with reset

I have a vector of numbers in a data.frame such as below. df <- data.frame(a = c(1,2,3,4,2,3,4,5,8,9,10,1,2,1)) I need to create a new column which gives a running count of entries that are greater than their predecessor. The resulting column…
nsymms
  • 115
  • 1
  • 1
  • 7
8
votes
3 answers

Running count based on field in R

I have a data set of this format User 1 2 3 2 3 1 1 Now I want to add a column saying count which counts the occurrence of the user. I want output in the below format. User Count 1 1 2 1 3 1 2 2 3 …
Gughan
  • 133
  • 3
  • 8
8
votes
3 answers

Creating a cumulative step graph in R

Say I have this example data frame set.seed(12345) n1 <- 3 n2 <- 10 n3 <- 60 times <- seq(0, 100, 0.5) individual <- c(rep(1, n1), rep(2, n2), rep(3, n3)) events <- c(sort(sample(times, n1)), …
nico
  • 50,859
  • 17
  • 87
  • 112
7
votes
3 answers

subquery or leftjoin with group by which one is faster?

i have to show running total with the total column in my application ... so i have used the following queries for finding the running total... and i find that both are working as per my need . in one i used the left join with group by and in…
rahularyansharma
  • 11,156
  • 18
  • 79
  • 135
7
votes
1 answer

How can I make a cumulative sum graph in grafana, from an elasticsearch data source?

Relevant info: Grafana v5.4.2 (commit: d812109) ElasticSearch version: 5.6.8 There is an ES index that is a log of events over time, and the events are categorised (per country). There is a grafana instance which has this ES index as a data…
1 2
3
95 96