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
4
votes
4 answers

Running total on multiple columns

I have a table like this. Date Item BuyItem 20150101 Mouse 10 20150101 Keyboard 100 20150202 Mouse 20 20150202 Keyboard 200 I want to…
Phyo Min Yu
  • 153
  • 1
  • 10
4
votes
1 answer

bounded Cumulative sum in Netezza

I know how to use cumulative sum in its basic formulation, with code like this: Table Name: Employees dept_id salary ------------- 10 1000 10 1000 10 2000 10 3000 20 5000 20 6000 20 NULL SELECT dept_id, salary, …
ADJ
  • 4,892
  • 10
  • 50
  • 83
4
votes
4 answers

Cumulative mean with conditionals

New to R. Small rep of my df: PTS_TeamHome <- c(101,87,94,110,95) PTS_TeamAway <- c(95,89,105,111,121) TeamHome <- c("LAL", "HOU", "SAS", "MIA", "LAL") TeamAway <- c("IND", "LAL", "LAL", "HOU", "NOP") df <- data.frame(cbind(TeamHome,…
Sburg13
  • 121
  • 5
4
votes
1 answer

Query to sum the previous values

I have the table strucure as in the image. I need to get the values added to the sum of previous values(shown in the REQUIRED RESULT) I tried with the following query SELECT empid, sum(tot_hours) OVER (PARTITION BY empid ORDER BY empid ) AS…
Shanna
  • 753
  • 4
  • 14
  • 34
4
votes
1 answer

Vector of cumulative sums in R

I'm trying to get a vector of cumulative sums, that is, I have: # 500 Samples from the U(0,1) Distribution U<-runif(500,0,1) # Empty Vector of length 500 F<-rep(0,500) # Fill the vector with f(U(k)) for ( i in 1:500 ){ F[i] <-…
WeakLearner
  • 918
  • 14
  • 26
4
votes
2 answers

Cannot cumulatively sum `COUNT(*)`

The second section of this answer uses variables to create a cumulative sum of another column. I'm doing the same thing, except that I am using a GROUP BY statement, and summing COUNT(*) instead of a column. Here is my code to create a minimal table…
Joel
  • 2,654
  • 6
  • 31
  • 46
4
votes
1 answer

Cumulative count of values in R

I hope you are doing very well. I would like to know how to calculate the cumulative sum of a data set with certain conditions. A simplified version of my data set would look like: t id A 22 A 22 R 22 A 41 A 98 A 98 A 98 R 98 A …
MSS
  • 53
  • 1
  • 8
4
votes
6 answers

SQL query for cumulative frequency of list of datetimes

I have a list of times in a database column (representing visits to a website). I need to group them in intervals and then get a 'cumulative frequency' table of those dates. For instance I might have: 9:01 9:04 9:11 9:13 9:22 9:24 9:28 and i want…
Simon
  • 525
  • 1
  • 14
  • 21
4
votes
1 answer

mysql query uses every row of another query

I have searched a lot, but cannot find a helpful answer: i want to have a list of totals from a period the user defines by giving me a start and end date. The totals should every time being from the start date to beginning with the start date and…
kvl
  • 43
  • 4
4
votes
2 answers

MySQL adding value from previous row

I have a table with a single column. The column is like this: 1 2 3 4 5 ... I want to create a query that will display another column that will have the previous value added to it. So: 1 1 ( 0 + 1 ) 2 3 ( 1 + 2 ) 3 5 ( 2 + 3 ) 4 7 ( 3 + 4 ) 5 9 ( 4…
nick
  • 2,743
  • 4
  • 31
  • 39
4
votes
2 answers

SQL Server Query, running total in view, reset when column A changes

A colleague of mine has a problem which I am trying to help him with. He has a SQL view which contains the following data (Sample data):- Category Value Cat A 10 Cat A 20 Cat A 30 Cat B 15 Cat…
general exception
  • 4,202
  • 9
  • 54
  • 82
3
votes
1 answer

Running sum on a column conditional on value

I have a vector of binary variables which state whether a product is on promotion in the period. I'm trying to work out how to calculate the duration of each promotion and the duration between promotions. promo.flag =…
Matt Weller
  • 2,684
  • 2
  • 21
  • 30
3
votes
1 answer

accumulated sum in query

How is it possible to return rows with an accumulate sum for a row bigger or smaller than a specified value? table: id | count ----------- 1 | 30 2 | 10 3 | 5 4 | 20 5 | 15 query: SELECT id, count FROM table ORDER BY id HAVING SUM(count) <…
clarkk
  • 27,151
  • 72
  • 200
  • 340
3
votes
4 answers

How to calculate overlapping subscription days from orders with sql-server

I have an ordertable with orders. I want to calculate the amount of subscriptiondays for each user (preffered in a set-based way) for a specific day. create table #orders (orderid int, userid int, subscriptiondays int, orderdate date) insert into…
Patrik
  • 241
  • 1
  • 3
  • 14
3
votes
1 answer

Polars cumulative sum over consecutive groups

I have a DataFrame like so: | Date | Group | Value | |------------|-------|-------| | 2020-01-01 | 0 | 5 | | 2020-01-02 | 0 | 8 | | 2020-01-03 | 0 | 9 | | 2020-01-01 | 1 | 5 | | 2020-01-02 | 1 | -1 | |…