cumulative-sum

Cumulating value of current row + sum of previous rows

試著忘記壹切 提交于 2019-11-27 17:45:12
问题 How would you do to transform a Column in a table from this: ColumnA ColumnB 2 a 3 b 4 c 5 d 1 a to this: ColumnA ColumnB 3 a 6(=3+3) b 10(=4+3+3) c 15(=5+4+3+3) d I'm interested to see esp. what method you would pick. 回答1: Like this: ;WITH cte AS ( SELECT ColumnB, SUM(ColumnA) asum FROM @t gROUP BY ColumnB ), cteRanked AS ( SELECT asum, ColumnB, ROW_NUMBER() OVER(ORDER BY ColumnB) rownum FROM cte ) SELECT (SELECT SUM(asum) FROM cteRanked c2 WHERE c2.rownum <= c1.rownum), ColumnB FROM

MySQL cumulative sum grouped by date

房东的猫 提交于 2019-11-27 14:06:52
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 stored with the hour minute and second that the interaction happened, so the group by is not grouping

How to calculate cumulative sum? [duplicate]

我的未来我决定 提交于 2019-11-27 09:54:39
This question already has an answer here: Calculating cumulative sum for each row 5 answers Vector of cumulative sums in R 1 answer 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 : biweek Total 0 3060.913 1 7455.076 2 10868.824 3 13786.372 4 17228.427 5 20576.825 6 22348.547 So it there a possible way to achieve it? # replace the second column for the cumsum of the initial second column data[, 2] <- cumsum(data

how to cumulatively add values in one vector in R

一世执手 提交于 2019-11-27 09:06:07
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 two things to this data set: first, I only want to preserve the row when the person became Boss for the

Why is my inclusive scan code 2x faster on CPU than on a GPU?

人走茶凉 提交于 2019-11-27 08:46:00
问题 I wrote a short CUDA program that uses the highly-optimized CUB library to demonstrate that one core from an old, quad-core Intel Q6600 processor (all four are supposedly capable of ~30 GFLOPS/sec) can do an inclusive scan (or cumulative/prefix sum if you rather) on 100,000 elements faster than an Nvidia 750 Ti (supposedly capable of 1306 GFLOPS/sec of single precision). Why is this the case? The source code is: #include "cuda_runtime.h" #include "device_launch_parameters.h" #include <cub/cub

SQL Server Cumulative Sum by Group

可紊 提交于 2019-11-27 07:54:06
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 In SQL Server 2005, I would do this using a correlated subquery: select dummy_id, date_registered, item_id, quantity, price, (select sum(quantity)

Cumulative count in R

核能气质少年 提交于 2019-11-27 04:58:57
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 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 passed to cumsum . You could replace id==id with rep(1,length(id)) . Here is a way to get the counts: id <- c(1,2

Calculate cumulative average (mean)

人走茶凉 提交于 2019-11-27 02:06:36
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. 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 just to compute the the mean for each position by summing over all previous values and dividing by their

Cumulative sum and percentage on column?

荒凉一梦 提交于 2019-11-26 22:41:17
问题 I have a DataFrame like this: df : fruit val1 val2 0 orange 15 3 1 apple 10 13 2 mango 5 5 How do I get Pandas to give me a cumulative sum and percentage column on only val1 ? Desired output: df_with_cumsum : fruit val1 val2 cum_sum cum_perc 0 orange 15 3 15 50.00 1 apple 10 13 25 83.33 2 mango 5 5 30 100.00 I tried df.cumsum() , but it's giving me this error: TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types

Calculating Cumulative Sum in PostgreSQL

北慕城南 提交于 2019-11-26 19:50:03
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 February 92576 1000 2014 1 1000 March 92573 3000 2014 1 4000 April 92570 2000 2014 1 6000 February