Coldfusion CF百分比列总和

Coldfusion CF百分比列总和,coldfusion,coldfusion-10,Coldfusion,Coldfusion 10,我在创建百分比列的总和时遇到了很多麻烦,这似乎很容易,因为当显示所有分支时,它是100%。但我需要计算出所有分支未显示的时间的等式。在下图中,每个分支的百分比是由处理的检查表的位置数除以检查表的总数计算的。不幸的是,我不知道如何只写百分比列的和,并显示在总计列中。任何帮助都将不胜感激 <cfset result = {} /> <cftry> <cfquery datasource="#application.dsn#" name="GetLocatio

我在创建百分比列的总和时遇到了很多麻烦,这似乎很容易,因为当显示所有分支时,它是100%。但我需要计算出所有分支未显示的时间的等式。在下图中,每个分支的百分比是由处理的检查表的位置数除以检查表的总数计算的。不幸的是,我不知道如何只写百分比列的和,并显示在总计列中。任何帮助都将不胜感激

<cfset result = {} /> 
<cftry> 
    <cfquery datasource="#application.dsn#" name="GetLocationInfo">
        SELECT *
        FROM cl_checklists
    </cfquery>

    <cfquery name="allLocCode" dbtype="query">
        SELECT DISTINCT trans_location, COUNT(*) AS locationCount FROM GetLocationInfo Where trans_location is not null GROUP BY trans_location ORDER BY trans_location
    </cfquery>
    <cfcatch type="any"> 
        <cfset result.error = CFCATCH.message > 
        <cfset result.detail = CFCATCH.detail > 
    </cfcatch> 
</cftry> 
    <cfset columnSum = ArraySum(allLocCode['locationCount'])>
<table border="1" id="Checklist_Stats">
    <thead>
        <th><strong>Location</strong></th>
        <th><strong>Percent of Total Checklists</strong></th>
        <th><strong>Location Total</strong></th> 
    </thead>
    <tbody>
     <cfloop query="allLocCode">
      <cfset thisLocationName = trim(allLocCode.trans_location) />

      <cfquery name="allLocCodeForLocationQry" dbtype="query">
          SELECT trans_location,count(*) AS locCntr FROM GetLocationInfo WHERE trans_location='#thisLocationName#' GROUP BY trans_location ORDER BY trans_location
      </cfquery>
      <cfoutput query="allLocCodeForLocationQry">
      <tr>
        <td><strong>#thisLocationName#</strong></td>
        <td>#NumberFormat((allLocCodeForLocationQry.locCntr/columnSum) * 100, '9.99')#%</td>
        <td>#allLocCodeForLocationQry.locCntr#</td>
      </tr>
     </cfoutput>
     </cfloop>
        <cfdump var="#allLocCodeForLocationQry.locCntr#">
     <tr>
      <td><strong>Total</strong></td>
      <td></td>
      <td><cfoutput>#columnSum#</cfoutput></td>
    </tr>
    </tbody>
</table>

我不确定如何得到这些问题的总和:NumberFormatallLocCodeForLocationQry.locCntr/columnSum*100,'9.99%

我之前对其中一个问题给出了类似的答案。这将使您达到您需要的位置:

每次获得百分比时,应将其设置为变量,并将其添加到列表或数组中。作为这段代码的示例:

 <cfloop query="allLocCode">
      <cfset thisLocationName = trim(allLocCode.trans_location) />
      <cfquery name="allLocCodeForLocationQry" dbtype="query">
          SELECT trans_location,count(*) AS locCntr FROM GetLocationInfo WHERE trans_location='#thisLocationName#' GROUP BY trans_location ORDER BY trans_location
      </cfquery>

      <cfset PercentageList = "">
      <cfset thisPercentage = #NumberFormat((allLocCodeForLocationQry.locCntr/columnSum) * 100, '9.99')#>
      <cfset PercentageList = ListAppend(PercentageList, thisPercentage, ',')>

      <cfoutput query="allLocCodeForLocationQry">
      <tr>
        <td><strong>#thisLocationName#</strong></td>
        <td>#thisPercentage#%</td>
        <td>#allLocCodeForLocationQry.locCntr#</td>
      </tr>
     </cfoutput>
 </cfloop>
在所有计算结束时,您应该有一个百分比列表。您可以使用此函数将列表添加到一起

<cfscript>
function listSum(listStr)
{
  var delim = ",";
  if(ArrayLen(Arguments) GTE 2) 
    delim = Arguments[2];
  return ArraySum(ListToArray(listStr, delim));
}
</cfscript>
因此,您的最后一行是:

<tr>
  <td><strong>Total</strong></td>
  <td>#listSum(PercentageList)#%</td>
  <td><cfoutput>#columnSum#</cfoutput></td>
</tr>
值得一提的是:之前有人向我指出,使用数组,然后在计算结束时转换为列表,可以获得更好的性能,所以这是需要记住的

是与此相关的函数

对于其他循环和查询中的所有循环和查询,我只是觉得创建一个列表更容易一些


E-无法解释的否决票不好。

您可以简单地将计算出的百分比推送到一个数组中,然后从那里可以得到如下所示的总和:

<!--- Define Array -->
<cfset checkListPercentage = arrayNew(1)>

<cfoutput query="allLocCodeForLocationQry">
  <cfset currentPercentage = allLocCodeForLocationQry.locCntr / columnSum * 100)>
  <cfset arrayAppend(checkListPercentage, currentPercentage)>
  <tr>
    <td><strong>#thisLocationName#</strong></td>
    <td>#numberFormat(currentPercentage, '9.99')#%</td>
    <td>#allLocCodeForLocationQry.locCntr#</td>
  </tr>
</cfoutput>

<!--- Get Total --->
<cfoutput>#arraySum(checkListPercentage)#</cfoutput>

这就是我想要表达的,但更简单、更有效@初学者可能更像专家。这基本上解决了这个问题。我如何显示2个小数点?是否可以同时使用数字格式和arraysum?@DavidBrierton是的,您可以。@DavidBrierton另外,请查看cfloop/cfoutput的group属性以优化代码。