Summing Dollar Amounts from Query- Evaluate? Loop? CFSET? Let SQL DO IT?
SQL Server 2008 R2
CF 9.0.1
Data type: money, null
Query name: get_payments
Fields:
payment_subtotal
payment_add
payment_subtract
Greetings
The best practice method to evaluate, add/subtract dollar sums to arrive at a grandTotal (that works) is tough to find.
I simply want to display the grand total on a list of orders in a "grandTotal" column.
Some advise doing the math in the DB,
select sum(payment_subtotal+payment_add-payment_subtract) as grandTotal
but this does not work because the query selects from 4 tables using left-Joins- where in the query would this appear?
others use
<cfset grandTotal = 0> <cfset grandTotal = grandTotal + (get_payments.payment_subtotal + get_payments.payment_add) etc.
or
<cfset grandTotal = ArraySum(get_payments['payment_subtotal'])
+ ArraySum(get_payments['payment_add']) etc.
or Loops:
<cfset grandTotal = 0>
<cfloop query="get_payments"><cfset grandTotal = grandTotal + ( #get_payments.payment_amount# + #get_payments.handling_cost# + #get_payments.Shipping_cost# ) />
single quotes, double quotes, with and without ## are not clear- and none of the above methods have been successful.
since the field could be null, I tried:
<cfoutput>
<cfif #get_payments.payment_subtotal# IS "">
<cfset get_payments.payment_subtotal=0>
<cfelseif #get_payments.payment_add# IS "">
<cfset get_payments.payment_add=0>
<cfelseif #get_payments.payment_subtract# IS "">
<cfset get_payments.payment_subtract=0>
</cfif>
</cfoutput>
The query results are structured:
<cfoutput query="get_payments">
<tr bgcolor="###iif(currentrow MOD 2,DE('ffffff'),DE('d9ecff'))#">
<td width="7%" class="tddynamic">#order_number#</td>
<td width="5%" class="tddynamic">#order_ID#</td>
<td width="7%" class="tddynamic">#DateFormat(payment_date, "mm/dd/yyyy")#</td>
<td width="7%" class="tddynamic">#payment_ID#</td>
<td width="15%" class="tddynamic">#client_company#</td>
<td width="20%" class="tddynamic">#order_property_street#, #order_property_city#</td>
<td width="15%" class="tddynamic">#appraiser_lname#</td>
<td width="7%" class="tddynamic">#DollarFormat(grandTotal)#</td>
<td width="5%" class="tddynamic">#payment_checknum#</td>
</tr>
</cfoutput>
Any help with the "foolproof" 2015 best practice would be appreciated.
Thank you in advance.
