• 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?
0
BongSoo
Asked:
BongSoo
  • 2
1 Solution
 
_agx_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
 
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+!
0
 
_agx_Commented:
Glad I could help :)  
0

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