cumulative-sum

Cumulative sum over a set of rows in mysql

别说谁变了你拦得住时间么 提交于 2019-11-26 17:51:53
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 1 2 1 10 1 2 2 40 1 2 2 30 2 1 1 10 2 1 2 15 2 1 3 30 2 2 1 10 2 2 2 20 2 2 2 30 I need to find cumulative total for each id, for every hour of the day. The output should be like this: id day hr amount cumulative total 1 1 1 10 10 1 1 2 25 35 1 1 3 30 65 1 2 1 10 10 1 2 2 40 50 1 2 2 30 80 2 1 1 10 10 2 1 2 15 25 2 1 3 30 55 2 2 1 10 10 2 2 2 20 30 2 2 2 30 60 My initial query that produces the

how to cumulatively add values in one vector in R

被刻印的时光 ゝ 提交于 2019-11-26 17:48:10
问题 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 1 1 Jane 1986 Boss 0 1 Jane 1987 Boss 0 2 Bob 1985 Worker 0 2 Bob 1986 Worker 0 2 Bob 1987 Manager 1 2 Bob 1988 Boss 0 2 Bob 1989 Boss 0 2 Bob 1990 Boss 0 2 Bob 1991 Boss 0 2 Bob 1992 Boss 0 Here, job2 denotes a dummy variable indicating whether a person was a Manager during that year or not. I want to do

MySQL cumulative sum grouped by date

吃可爱长大的小学妹 提交于 2019-11-26 16:36:05
问题 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 i have SELECT e.Date AS e_date, count(e.ID) AS num_interactions FROM example AS e JOIN example e1 ON e1.Date <= e.Date GROUP BY e.Date; The output of this is close to what I want but not exactly what I need. the problem I'm having is the dates are

SQL Server Cumulative Sum by Group

房东的猫 提交于 2019-11-26 13:57:16
问题 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 date_registered item_id quantity price cumulative 1 2013-07-01 100 10 34.5 10 2 2013-07-01 145 8 2.3 8 3 2013-07-11 100 20 34.5 30 4 2013-07-23 100 15 34.5 45 5 2013-07-24 145 10 34.5 18 Thanx in advance 回答1: In SQL Server 2005, I would do this using

Cumulative count in R

末鹿安然 提交于 2019-11-26 09:54:47
问题 Is there a way of counting the number of times an object appears in a column cumulatively in R? 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 2 etc... Thanks 回答1: The ave function computes a function by group. > id <- c(1,2,3,2,2,1,2,3) > data.frame(id,count=ave(id==id, id, FUN=cumsum)) id count 1 1 1 2 2 1 3 3 1 4 2 2 5 2 3 6 1 2 7 2 4 8 3 2 I use id==id to create a vector of all TRUE values, which get converted to numeric when

R cumulative sum by condition with reset

久未见 提交于 2019-11-26 08:27:14
问题 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 vector should be this: 0,1,2,3,0,1,2,3,4,5,6,0,1,0 My attempt is to create a \"flag\" column of diffs to mark when the values are greater. df$flag <- c(0,diff(df$a)>0) > df$flag [1] 0 1 1 1 0 1 1 1 1 1 1 0 1 0 Then I can apply some dplyr group/sum magic

Calculating Cumulative Sum in PostgreSQL

霸气de小男生 提交于 2019-11-26 07:28:00
问题 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 92571 3000 2014 2 April 92572 2000 2014 3 March 92573 3000 2014 1 March 92574 2500 2014 2 March 92575 3750 2014 3 February 92576 2000 2014 1 February 92577 2500 2014 2 February 92578 1450 2014 3 I want my target table to look something like this: ea_month id amount ea_year circle_id cum_amt

Calculate cumulative average (mean)

蹲街弑〆低调 提交于 2019-11-26 05:39:37
问题 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 as a result. Now, how to do the cumulative average of these numbers. 回答1: In analogy to the cumulative sum of a list I propose this: The cumulative average avg of a vector x would contain the averages from 1st position till position i . One method is

Cumulative sum over a set of rows in mysql

[亡魂溺海] 提交于 2019-11-26 05:39:00
问题 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 1 2 1 10 1 2 2 40 1 2 2 30 2 1 1 10 2 1 2 15 2 1 3 30 2 2 1 10 2 2 2 20 2 2 2 30 I need to find cumulative total for each id, for every hour of the day. The output should be like this: id day hr amount cumulative total 1 1 1 10 10 1 1 2 25 35 1 1 3 30 65 1 2 1 10 10 1 2 2 40 50 1 2 2 30 80 2 1