CF查询删除空字符串结果总和

时间:2016-04-05 13:38:05

标签: coldfusion coldfusion-10

我正在尝试修复我的查询,以便我的总和列等于正确的数字。我尝试将此行<cfset columnSum = ArraySum(allLocCode['locationCount'])>更改为<cfset columnSum = ArraySum(trim(allLocCode['locationCount']))>但是通过错误。我希望像下面图片中的空字符串不计入总数,就像它没有在表格中显示一样。是否有其他方法可以为我的总列提取这个修剪?

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

    <cfcatch type="any"> 
        <cfset result.error = CFCATCH.message > 
        <cfset result.detail = CFCATCH.detail > 
    </cfcatch> 
</cftry> 

<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>
    <cfquery name="allLocCode" dbtype="query">
        SELECT DISTINCT trans_location, COUNT(*) AS locationCount FROM GetLocationInfo GROUP BY trans_location ORDER BY trans_location 
    </cfquery>
     <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/allLocCode.locationCount) * 100, '9.99')#%</td>
        <td>#allLocCodeForLocationQry.locCntr#</td>
      </tr>
     </cfoutput>
     </cfloop>
        <cfset columnSum = ArraySum(allLocCode['locationCount'])>
     <tr>
      <td><strong>Total</strong></td>
      <td></td>
      <td><cfoutput>#columnSum#</cfoutput></td>
      <cfdump var="#allLocCode#">
      <cfdump var="#allLocCodeForLocationQry#">
      <cfdump var="#thisLocationName#">
    </tr>
    </tbody>
    <!--- Total of All Sum of each column --->
</table>

enter image description here

正确答案应反映334而不是340

1 个答案:

答案 0 :(得分:0)

根据要求,以下是相关代码的答案:

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

如果你需要更多关于百分比的帮助,我建议你开一个新帖子。我从你的历史中看到你已经有几个与这个功能相关的线程,并且随着它变得越来越复杂,它将有助于分离所有内容。

相关问题