Coldfusion 如何计算列和

Coldfusion 如何计算列和,coldfusion,sum,row,cfml,Coldfusion,Sum,Row,Cfml,我以前问过这样的问题,但没有得到任何好的答案,可能是因为代码太长或者我的问题不清楚。这一次,我将尽我所能:)到目前为止,我已经编写了从表中查找行和的代码,这很好: <cfloop list="#product_id_list#" index="product_index"> <cfloop list="#month_list#" index="month_index"> <cfoutput query="GET_SALES_TOTAL">

我以前问过这样的问题,但没有得到任何好的答案,可能是因为代码太长或者我的问题不清楚。这一次,我将尽我所能:)到目前为止,我已经编写了从表中查找行和的代码,这很好:

<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>

#TLFormat(evaluate('total#kk#u ind#uu#p#u index#'),0)#
和列总和的输出:

<cfloop list="#month_list#" index="kk">
 <td align="center">
   <cfset satis_oran= evaluate('total2_#kk#_#p_index#')>
     #evaluate(satis_oran)#
 </td>
</cfloop>

#评估(satis_oran)#

我知道我没有按产品id循环列输出,因为一旦我循环它,它会生成大量的
,这意味着大量不相关的数据。这里可能有什么错误

这很难理解

一些建议

尝试在sql语句中执行此操作

您可以简单地使用GROUP语句对所有这些值求和。类似于

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)
如果不是所有月份或产品都在返回的查询中,那么这是确定的。您仍然可以在几个月后对产品进行循环

不要使用evaluate

据我所知,CF实际上是在动态编译,速度非常慢。如果需要动态引用变量名,请使用范围和括号。如果您实际上是在保存语句以供以后评估,那么可能还有其他选择

不要使用字体标签


我可能在过去6年里没有使用过字体标签。除非处理一些依赖于它的遗留代码,否则不应使用字体标记。

这很难理解

一些建议

尝试在sql语句中执行此操作

您可以简单地使用GROUP语句对所有这些值求和。类似于

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)
如果不是所有月份或产品都在返回的查询中,那么这是确定的。您仍然可以在几个月后对产品进行循环

不要使用evaluate

据我所知,CF实际上是在动态编译,速度非常慢。如果需要动态引用变量名,请使用范围和括号。如果您实际上是在保存语句以供以后评估,那么可能还有其他选择

不要使用字体标签


我可能在过去6年里没有使用过字体标签。除非处理依赖于它的某些旧代码,否则不应使用字体标记。

如果查询中有一列,并且可以确保每个值都是数字,还可以执行以下操作:

<cfset sum = arraySum(queryname['column'])>


但是,如果它遇到任何非数值,则会导致错误,因此您可能需要在该字段或其他内容周围放置一条合并语句,以确保任何空值都转换为零。

如果您的查询中有一列,并且您可以确保每个值都是数值,您还可以执行以下操作:

<cfset sum = arraySum(queryname['column'])>


但是,如果它遇到任何非数字值,则会导致错误,因此您可能需要在该字段或其他内容周围放置一个coalesce语句,以确保任何空值都转换为零。

根据表列的数据类型,请在较旧的CF版本中尝试以下操作:

<cfset theSum = ArraySum(ListToArray(ValueList(queryName.column))) />

根据表格列的数据类型,请在较旧的CF版本中尝试以下操作:

<cfset theSum = ArraySum(ListToArray(ValueList(queryName.column))) />


coldfusionvalcolumn中没有此类valcolumn valcolumn是一个示例列名,而不是文字!在SQL中执行此操作也会有更高的性能。我会对您的查询做更多的工作,按productid分组,然后按月获取您的循环。。。把所有的辛苦工作放回到它设计的db a上。coldfusionvalcolumn中没有这样的valcolumn valcolumn是一个示例列名而不是文字!在SQL中执行此操作也会有更高的性能。我会对您的查询做更多的工作,按productid分组,然后按月获取您的循环。。。把所有的辛苦工作都放回到它设计的db a上。或者
arraysum(valuearray(queryname.column))
(特别是在与Railo合作时)。这应该被标记为正确答案。或者
arraysum(valuearray(queryname.column))
(特别是在与Railo合作时)。这应该被标记为正确答案。