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

Cumulative difference In R

What is the opposite of cumsum() in R a = c(2, 5, 8) cumsum = c(2, 7, 15) cumdiff = c(2, 3, 1) Because 5-2 = 3 and 8-7 = 1. Is there a package that can be used for this one in R?
Lilac_Mimo
  • 315
  • 5
4
votes
3 answers

numpy cumulative count in linear time

I have data like this: [144 144 144 144 143 143 143 93 93 93 93 93 93 93 93 93 93], and I want to make data like this: [0, 1, 2, 3, 0, 1, 2, 0, 1, 2, 3, 4, ....] I tried to use the function in the link below, I got this: [3 2 1 0 2 1 0 9 0…
UNGGI LEE
  • 69
  • 3
4
votes
1 answer

Compute the cumulative volume within the session in pine-script

I want to compute the cumulative volume of bars - within each trading session - in pine-script (TradingView.com). I wrote the script below but I get error "Script could not be translated from: for i = 1 to session_bar_counter" I've tried the below…
Silviu
  • 135
  • 1
  • 9
4
votes
3 answers

How to get running total from consecutive columns in Oracle SQL

I have troubles to display consecutive holidays from an existing date dataset in Oracle SQL. For example, in December 2017 between 20th and 30th, there are the following days off (because Christmas and weekend days): 23.12.2017 Saturday 24.12.2017…
Ivo
  • 303
  • 2
  • 15
4
votes
2 answers

Cumulating dates in SQL

I have this table with this sample data: DECLARE @Sample TABLE (fill_date DATETIME, days_supplied INT) INSERT INTO @Sample (fill_date, days_supplied) VALUES ( '02/17/2005', --> DATEADD(dd, 500, '02/17/2005') = '07/02/2006' 500 ), …
joey0xx
  • 73
  • 10
4
votes
3 answers

Cumulative percentages in R

I have the following data frame d2 # A tibble: 10 x 2 ID Count 1 1 2 1 3 1 4 1 5 1 6 2 7 2 8 2 9 3 10 3 Which states how many counts each person (ID) had. I would like to…
Omry Atia
  • 2,411
  • 2
  • 14
  • 27
4
votes
2 answers

R: how to add volumes from date time

I have the following data: df <- Session Volume StartTime EndTime 1 27,75 2016-01-22 17:00:33.707 2016-01-27 06:02:54.900 2 10,78 2016-01-22 14:31:22.127 2016-01-23…
ima
  • 155
  • 12
4
votes
4 answers

sql dynamic cumulative sum - SQL Server

I have a table like this +------+--------+ | ID | Salary | +------+--------+ | 1 | 100 | | 2 | 40 | | 3 | 30 | | 4 | 40 | | 5 | 90 | | 6 | 160 | | 7 | 70 | | 8 | 40 | | 9 | 20 | | 10 …
Gork. O
  • 65
  • 7
4
votes
2 answers

Rolling 12 month Sum in PostgreSQL

I need to be able to create a Trailing Twelve Month report using SQL (PostgreSQL) - essentially a window/rolling 12 month sum that sums up the current month's totals + the previous 11 months for each month. I have this table: CREATE TABLE…
jsxgd
  • 403
  • 1
  • 5
  • 16
4
votes
4 answers

Java - Perform a cumulative sum using forEach or the Stream API

here my current code to perform a cumulative sum over a hash table Map< String,Double> START.forEach((k,v)->{ sum += v; END.put(k, sum); }); or, alternately, END= START.entrySet() .stream() …
Fab
  • 1,145
  • 7
  • 20
  • 40
4
votes
1 answer

Pandas: Cumulative sum of one column based on value of another

I am trying to calculate some statistics from a pandas dataframe. It looks something like this: id value conditional 1 10 0 2 20 0 3 30 1 1 15 1 3 5 0 1 10 1 So, I…
sfactor
  • 12,592
  • 32
  • 102
  • 152
4
votes
1 answer

How to show order fulfilment in a SQL Server 2008 query

I am trying to think of a way on a SQL Server 2008 database to run through a sales order table and get open demand for a part, order it by due date, then look at a purchase order table and fulfill the sales orders by PO, ordering the PO supply by…
jenhil34
  • 1,451
  • 3
  • 17
  • 27
4
votes
1 answer

Cumulative Ranking of Values in Pandas with Ties

I am trying to find a way to do a cumulative total that accounts for ties in Pandas. Lets take hypothetical data from a track meet, where I have people, races, heats, and time. Each person's placement is according to the following: For a given…
4
votes
4 answers

SQL Server - Running Total with Carry Forward

Needs some help on the following: Table #Data contains the Opening and Closing Stock for a product over 5 days Table #BackData contains some post dated transactions How can i Update the table #Data with a Running Total including a carry…
4
votes
3 answers

Conditional numpy cumulative sum

I'm looking for a way to calculate the cumulative sum with numpy, but don't want to roll forward the value (or set it to zero) in case the cumulative sum is very close to zero and negative. For instance a = np.asarray([0, 4999, -5000,…
orange
  • 7,755
  • 14
  • 75
  • 139