How to calculate column sum
I asked this kind of question before, but didn't receive any good answers, perhaps because the code was too long or my question was unclear. For this time I will try to do my best :) So far I've written code to find from a table the row sum, which works fine:
<cfloop list="#product_id_list#" index="product_index">
<cfloop list="#month_list#" index="month_index">
<cfoutput query="GET_SALES_TOTAL">
<cfif AY eq 开发者_运维百科month_index and product_id eq product_index>
<cfloop list="#type_index#" index="tt_index">
<cfset 'alan_#tt_index#_#month_index#_#product_index#' = evaluate(tt_index)>
</cfloop>
</cfif>
</cfoutput>
</cfloop>
</cfloop>
<cfset 'total_#ii_index#_#p_index#'=evaluate('total_#ii_index#_#p_index#') + #evaluate('alan_#ii_index#_#ddd_other#_#p_index#')#>
Now I want to find a column sum. The code for the column sum works, but incorrectly. It counts the sum of the last product:
<cfloop list="#product_id_list#" index="product_index">
<cfloop list="#month_list#" index="month_index">
<cfoutput query="GET_SALES_TOTAL">
<cfif AY eq month_index and product_id eq product_index>
<cfloop list="#type_index#" index="tt_index">
<cfset 'alan2_#tt_index#_#month_index#_#product_index#' = evaluate(tt_index)>
</cfloop>
</cfif>
</cfoutput>
</cfloop>
</cfloop>
<cfset 'total2_#ddd_other#_#p_index#'=evaluate('total2_#ddd_other#_#p_index#') + #evaluate('alan2_#ii_index#_#ddd_other#_#p_index#')#>
The output for the row sum:
<cfloop list="#product_id_list#" index="p_index">
<cfloop list="#type_index#" index="kk_ind">
<td align="center">
<font color="##FF0000">#TLFormat(evaluate('total_#kk_ind#_#p_index#'),0)#</font>
</td>
</cfloop>
</cfloop>
and the output for column sum:
<cfloop list="#month_list#" index="kk">
<td align="center">
<cfset satis_oran= evaluate('total2_#kk#_#p_index#')>
#evaluate(satis_oran)#
</td>
</cfloop>
I know that I didn't loop the column output by product id, because once I loop it, it generates a lot of <td>
's, meaning a lot of irrelevant data. What can be the mistake here?
If you have a column in your query, and you can ensure each value will be numeric, you can also do:
<cfset sum = arraySum(queryname['column'])>
If it encounters any non-numeric values though, it will cause an error, so you probably need to put a coalesce statement around that field or something to make sure any null values get converted to zero.
This is very hard to follow.
Some suggestions....
Try to do this in sql statement
You may be able to simply use a GROUP statement to sum all of these values. Something like...
select productindex
, datepart('yyyy', datecolumn) as year
, datepart('mm', datecolumn) as month
, sum(valcolumn) as valcolumnsum
from productinfo
group by productindex, datepart('yyyy', datecolumn), datepart('mm', datecolumn)
If not all months or products are actually in the returned query that is ok. You can still loop over products and months later.
Don't use evaluate
It is my understanding that CF is literally compiling on the fly which is very slow. If you need to reference a variable name dynamically use a scope and brackets. If you are actually saving statements to be evaluated later there are probably alternatives
Don't use font tags
I haven't used a font tag in probably the last 6 years. Unless working on some legacy code dependent on it the font tag should not be used.
Depending on the datatype of the table column, try this for older CF versions:
<cfset theSum = ArraySum(ListToArray(ValueList(queryName.column))) />
For Lucee Versions
listArray()
and valueList()
functions are deprecated.
Therefore, the best solution would be the following:
arraySum(queryName.columnData('queryColumnName'))
精彩评论