• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1045
  • 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 :)  
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

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