CF 百分比总和列

CF sum of percentage column

我在创建我的百分比列的总和时遇到了很多麻烦,这看起来很容易,因为当显示所有分支时它是 100%。但是我需要找出所有分支都没有显示的时间的等式。在下图中,每个分支百分比的计算方法是处理清单的位置数除以清单总数。不幸的是,我不知道如何 "write" 将百分比列的总和显示在总计列中。任何帮助将不胜感激。

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

不确定如何得到这个总和:<td>#NumberFormat((allLocCodeForLocationQry.locCntr/columnSum) * 100, '9.99')#%</td>

我之前针对其中一个问题发布了一个有点类似的答案。这将带您到达目的地:


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

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

值得一提:之前,有人向我指出,使用数组并 then 在计算结束时转换为列表会产生更好的性能,所以这是记住。

ArraySum 是相关函数。

在其他循环和查询中包含所有循环和查询,我只是觉得创建列表更容易一些。

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>