Wednesday, March 28, 2012

runningsum

Hi,

I have this problem. I need make a sql statment (not a SP) for this:

Example:

date amount
1/1 10
1/2 5
1/3 20

Now, my output is:

date amount
1/1 10
1/2 15 (5+10)
1/3 35 (15+15)

any idea? in access can make this with a runningsum,:eek: but, in SQL 2000?

TIA:beer:
Abel.I found a solution, with a subqueris, example
select date, amount, O.amount
,(select sum(amount) from <myTable>
where date <= O.date)
'runningSum'
from <myTable> O

another idea?
thanks.
Abel.|||Alternate method:

select TBL1.date,
sum(TBL2.Amount) as RunningSum
from [MyTable] TBL1
inner join [MyTable] TBL2 on TBL1.date >= TBL2.date
group by TBL1.date|||there are a couple of different sources that say a cursor (horrors!) is actually more efficient that set-based in the case of running totals:
http://www.sqlteam.com/article/calculating-running-totals
http://sqljunkies.com/WebLog/amachanic/archive/2006/02/28/18286.aspx

but I guess the fastest way is to do it in a CLR proc:
http://sqljunkies.com/WebLog/amachanic/archive/2006/02/28/18309.aspx|||Thanks blindman and jezmine.

jezmine i read this link and tell to you. thanks again.

No comments:

Post a Comment