running-total

List comprehension for running total

别来无恙 提交于 2019-11-27 19:23:50
I want to get a running total from a list of numbers. For demo purposes, I start with a sequential list of numbers using range a = range(20) runningTotal = [] for n in range(len(a)): new = runningTotal[n-1] + a[n] if n > 0 else a[n] runningTotal.append(new) # This one is a syntax error # runningTotal = [a[n] for n in range(len(a)) if n == 0 else runningTotal[n-1] + a[n]] for i in zip(a, runningTotal): print "{0:>3}{1:>5}".format(*i) yields 0 0 1 1 2 3 3 6 4 10 5 15 6 21 7 28 8 36 9 45 10 55 11 66 12 78 13 91 14 105 15 120 16 136 17 153 18 171 19 190 As you can see, I initialize an empty list [

Calculate balance with mysql

為{幸葍}努か 提交于 2019-11-27 13:21:00
问题 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 which gives me the following result: ID In Out Balance 1 100.00 0.00 100.00 2 10.00 0.00 110.00 3 0.00 70.00 40.00 4 5.00 0.00 45.00 5 0.00 60.00 -15.00 6 20.00 0.00 5.00 Is it possible to do this with one query, without using a trigger or stored procedures? 回答1: Short answer, yes Longer answer, you can use a variable to tally it up as it

Running Sums for Multiple Categories in MySQL

放肆的年华 提交于 2019-11-27 02:31:25
问题 I have a table of the form Category Time Qty A 1 20 B 2 3 A 3 43 A 4 20 B 5 25 I need a running total to be calculated by category in MySQL. The result would look something like this: Category Time Qty Cat.Total A 1 20 20 B 2 3 3 A 3 43 63 A 4 20 83 B 5 25 28 Any idea how I could do this efficiently in MySQL? I have searched far and wide, but all I can find is info on how to insert one single running total in MySQL. I wonder if there's any way to use GROUP BY or a similar construct to achieve

subquery or leftjoin with group by which one is faster?

此生再无相见时 提交于 2019-11-26 23:02:20
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 another one i used the sub query . and now my question is which one is faster when my data grow in thousands daily and if data will be in limit of 1000 or 2000 rows then which one is better ... and any other method by which is more faster then these two ???? declare @tmp table(ind int identity(1,1),col1 int) insert into @tmp select 2 union select 4 union

List comprehension for running total

旧时模样 提交于 2019-11-26 19:51:17
问题 I want to get a running total from a list of numbers. For demo purposes, I start with a sequential list of numbers using range a = range(20) runningTotal = [] for n in range(len(a)): new = runningTotal[n-1] + a[n] if n > 0 else a[n] runningTotal.append(new) # This one is a syntax error # runningTotal = [a[n] for n in range(len(a)) if n == 0 else runningTotal[n-1] + a[n]] for i in zip(a, runningTotal): print "{0:>3}{1:>5}".format(*i) yields 0 0 1 1 2 3 3 6 4 10 5 15 6 21 7 28 8 36 9 45 10 55

subquery or leftjoin with group by which one is faster?

家住魔仙堡 提交于 2019-11-26 08:30:45
问题 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 another one i used the sub query . and now my question is which one is faster when my data grow in thousands daily and if data will be in limit of 1000 or 2000 rows then which one is better ... and any other method by which is more faster then these two ?

Create a Cumulative Sum Column in MySQL

核能气质少年 提交于 2019-11-26 01:19:01
问题 I have a table that looks like this: id count 1 100 2 50 3 10 I want to add a new column called cumulative_sum, so the table would look like this: id count cumulative_sum 1 100 100 2 50 150 3 10 160 Is there a MySQL update statement that can do this easily? What\'s the best way to accomplish this? 回答1: If performance is an issue, you could use a MySQL variable: set @csum := 0; update YourTable set cumulative_sum = (@csum := @csum + count) order by id; Alternatively, you could remove the

Calculate a Running Total in SQL Server

偶尔善良 提交于 2019-11-25 22:14:31
问题 Imagine the following table (called TestTable ): id somedate somevalue -- -------- --------- 45 01/Jan/09 3 23 08/Jan/09 5 12 02/Feb/09 0 77 14/Feb/09 7 39 20/Feb/09 34 33 02/Mar/09 6 I would like a query that returns a running total in date order, like: id somedate somevalue runningtotal -- -------- --------- ------------ 45 01/Jan/09 3 3 23 08/Jan/09 5 8 12 02/Feb/09 0 8 77 14/Feb/09 7 15 39 20/Feb/09 34 49 33 02/Mar/09 6 55 I know there are various ways of doing this in SQL Server 2000 /