Queries and Totals in ColdFusion

While reading the CF-Talk e-mails, I noticed a couple of interesting, yet simple, facts about queries and totals in ColdFusion. First, in any query, queryName["columnName"] returns an array of columnName values. Second, if you want to calculate the sum of columnName values, you can simply use the function arraySum(). Here is an example (tested on CFMX7):

<cfset q = queryNew("col1", "integer")>

<cfset queryAddRow(q,4)>

<cfset querySetCell(q,"col1",1,1)>
<cfset querySetCell(q,"col1",2,2)>
<cfset querySetCell(q,"col1",3,3)>
<cfset querySetCell(q,"col1",4,4)>

<cfdump var="#q#">

<cfloop from="1" 
        to="#arrayLen(q['col1'])#" 
        index="i">
    <cfoutput>
        #i#
    </cfoutput>
    <br>
</cfloop>
<cfoutput>
    #arraySum(q["col1"])#
</cfoutput>

(Thanks Deanna Schneider)


Possibly related:


Tagged | Post a Comment