Possible Duplicate:
MySQL query that computes partial sums
ok, the question is not clear at all so the example would be:
i have a table like this:
id amount
1 1000
2 2500
3 5000
and i need to select from it the following data:
id oamount total
1 1000 1000
2 2500 3500
3 5000 8500
i tried this but it is not correct:
select *,sum(oamount) from `table`
group by id;
and i can’t figure it out
I’ve answered a very similar where they were trying to get cash flow balances for beginning / ending of each day… Found here
Yours would be very similar… Prequery the data in the final order you want it (ie: starting ID would be the first of the result set), then apply what you want with MySQL Variables
I’ve actually kept the pre-aggregate as a query… in case you wanted to actually DO some aggregations like based on transactions on a daily basis, or applied some other WHERE clause. This would allow flexibility to ensure your final output order was prepared BEFORE starting your @PrevBal accumulation.