2 Replies Latest reply on Jul 26, 2006 6:11 PM by Daniel_Eng

# Looping Incremental Value

Hi, I would like to produce the following results:

DATABASE VALUE / CALCULATED VALUE
A / A
B / A + B
C / A + B + C
D / A + B + C + D
.....

I have a bunch of numbers in a field, and I would like to produce the results as shown above. To show the first value, then followed by the sum of number of all previous numbers before that value.

Can someone recommend an effective way of doing this?
• ###### 1. Re: Looping Incremental Value
There's a contradiction in your question. "then followed by the sum of number of all _PREVIOUS_ numbers _BEFORE_ that value.", and you're still giving "D / A + B + C + D" as an example. So, do you want to include the _current_ value or not?

Also, you're saying don't want the sum of all previous numbers, but the sum of _NUMBER_OF_ previous numbers? Eh?
So, first iteration would produce 1, the second 1+2, third 1+2+3, fourth 1+2+3+4, etc?

I'm puzzled, but maybe here's some help:

Assuming we use something like "SELECT databaseValue FROM myTable" ...

<CFSET sumOfPrevious = 0>
<CFOUTPUT query="myQuery">
<CFSET sumOfPrevious = sumOfPrevious + databaseValue>
#databaseValue# / #databaseValue/sumOfPrevious#
<BR>
</CFOUTPUT>

If I got you wrong, and you want to divide the value by the sum of all _PREVIOUS_ numbers, move the <CFSET> after the <CFOUTPUT> line just before the </CFOUTPUT>, so that the result is printed out first.

If I got you wrong again, and you want to divide the value by the *sum* of _number_of_values_ (it's sounds insane, I know), increment the sumOfPrevious by #myQuery.currentrow# each time, instead of the databaseValue.
• ###### 2. Re: Looping Incremental Value
Thanks Fernis, the first one is the one that I wanted. Wanted to show the number on the left hand side followed by the sum of numbers of all the numbers of the previous row up to the current row. It worked fine.

Thanks again!