Add

As it happens, I had the need today to add a totals row to an existing query object that summed the various column data above it. Like this:

Month Oranges
January 5
February 12
March 3
April 8
May 14
June 11
July 6
August 11
September 10
October 12
November 7
December 4
Total Oranges Sold 103

In this particular case, I preferred to do the totalling in the cfc where i’m building the query, rather than after the fact in the cfm page. I didn’t figure it would be difficult, but as it turns out, there was a bit of a hitch in the way one can reference query object columns from the ArraySum function (specifically, dot notation doesn’t work). But, then I found Ben Nadel’s post on this issue wherein he used array notation, and I’m now good to go. Thanks, Ben. Hope this little tidbit helps someone else.

<cfquery name="orangesales" datasource="#request.ds#">
    SELECT monthname, oranges
    FROM orangesales
</cfquery>

<cfset QueryAddRow(orangesales) />
<cfset QuerySetCell(orangesales,"monthname","Total Oranges Sold") />
<cfset QuerySetCell(orangesales,"oranges",ArraySum(orangesales["oranges"])) />

Comments are closed.