• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1045

# Coldfusion Math

I am oversimplifying so hopefully this will make sense:

If I have a query that returns to columns of numbers:

points     modifier
100        2
50          3
25          1

and I want to do the math such that

100 x 2
50 x 3
25 x 1

and then add up those totals (200 + 150 + 25) how can I do that?
0
BongSoo
• 2
1 Solution

Commented:
One option is to do the first calculation in your query sql:

<cfquery name="yourQuery" datsource="....">
SELECT   Points, Modifier, (Points * modifier) AS Total
FROM     YourTable
WHERE    ....
</cfquery>

Then use either a QoQ to calculate the grand total

<cfquery name="getGrandTotal" dbtype="query">
SELECT   SUM(Total) AS GrandTotal
FROM     yourQuery
</cfquery>

<cfoutput><b>grand total=</b> #getGrandTotal.grandTotal#</cfoutput>

... Or calculate the grand total inside your output loop

<cfset grandTotal = 0>
<cfoutput query="yourQuery">
points=#points# modifier=#modifier# total=#total#<br>
<cfset grandTotal = grandTotal + total>
</cfoutput>
<cfoutput><b>grand total=</b> #grandTotal#</cfoutput>
0

Author Commented:
Awesome! I was trying to figure out how to do the (Points * Modifier) AS Total, but I had it screwed up! Plus, your QofQ works better than what I was trying to do.

Thanks for the quick response! A+!
0

Commented: