如何计算列总和
我以前问过此类问题,但没有得到任何好的答案,可能是因为代码太长或者我的问题不清楚。这次我将尽力做到最好:)到目前为止,我已经编写了从表中查找行总和的代码,效果很好:
<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#')#>
现在我想找到列总和。列总和的代码有效,但不正确。它计算最后一个产品的总和:
<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#')#>
行总和的输出:
<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>
和列总和的输出:
<cfloop list="#month_list#" index="kk">
<td align="center">
<cfset satis_oran= evaluate('total2_#kk#_#p_index#')>
#evaluate(satis_oran)#
</td>
</cfloop>
我知道我没有按产品 id 循环输出列,因为一旦我循环它,它就会生成很多 < code> 的,意味着很多不相关的数据。这里可能有什么错误?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果查询中有一个列,并且可以确保每个值都是数字,您还可以这样做:
如果遇到任何非数字值,则会导致错误,因此您可能需要放置一个合并语句该字段或其他内容以确保任何空值都转换为零。
If you have a column in your query, and you can ensure each value will be numeric, you can also do:
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.
这很难遵循。
一些建议......
尝试在 sql 语句中执行此操作
您也许可以简单地使用 GROUP 语句来对所有这些值求和。类似...
如果不是所有月份或产品实际上都在返回的查询中,那就可以了。几个月后,您仍然可以循环查看产品。
不要使用评估
据我所知,CF实际上是动态编译,速度非常慢。如果需要动态引用变量名,请使用范围和括号。如果您实际上要保存语句以供稍后评估,则可能有其他选择
不使用字体标签
我可能在过去 6 年里没有使用过字体标签。除非处理一些依赖于它的遗留代码,否则不应使用字体标签。
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...
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.
根据表列的数据类型,对较旧的 CF 版本尝试以下操作:
Depending on the datatype of the table column, try this for older CF versions:
对于 Lucee 版本
listArray()
和valueList()
函数已弃用。因此,最好的解决方案如下:
For Lucee Versions
listArray()
andvalueList()
functions are deprecated.Therefore, the best solution would be the following: