• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1039
  • Last Modified:

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?
  • 2
1 Solution
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    ....

Then use either a QoQ to calculate the grand total

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

  <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><b>grand total=</b> #grandTotal#</cfoutput>
BongSooAuthor 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+!
Glad I could help :)  

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now