Coldfusion CF在数据库查询中获取列的总和
我正在尝试使用ColdFusion和MS Sql计算列的总数 有人能告诉我我忽略了什么吗?我对位置总数的合计不是对该列求和,而是出于某种原因,只取该列中的最后一个数字。我哪里做错了Coldfusion CF在数据库查询中获取列的总和,coldfusion,coldfusion-10,Coldfusion,Coldfusion 10,我正在尝试使用ColdFusion和MS Sql计算列的总数 有人能告诉我我忽略了什么吗?我对位置总数的合计不是对该列求和,而是出于某种原因,只取该列中的最后一个数字。我哪里做错了 <cfset result = {} /> <cftry> <cfquery datasource="#application.dsn#" name="GetLocationInfo"> SELECT * FROM cl_checklis
<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(trans_location) AS locCntr FROM GetLocationInfo WHERE trans_location='#thisLocationName#' GROUP BY trans_location ORDER BY trans_location
</cfquery>
<cfset columnSum = ArraySum(allLocCodeForLocationQry['locCntr'])>
<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>
<tr>
<td><strong>Total</strong></td>
<td></td>
<td><cfoutput>#numberFormat(columnSum)#</cfoutput></td>
</tr>
</tbody>
<!--- Total of All Sum of each column --->
</table>
试试这个
每次循环查询时,都会将一个值(位置计数)附加到列表中
大概是这样的:
<cfset myList = "">
<cfloop query="allLocCode">
<cfset myList = ListAppend(myList, locationCount, ',')>
<!---your other logic--->
</cfloop>
最后的答案是121。columnSum变量在cfloop中。将该行放在cfloop的前面或后面,您将得到总计334行。既然这是sql server,为什么不利用它使用with关键字的能力呢?总体思路是:
with totalRecords as
(select count(*) records
from etc),
groupedRecords as
(select someField, count(*) recordsForField
from etc
group by someField)
select whatevever
, (groupedRecords.recordsForField / totalRecords.records) * 100 percentage
from someTables
join groupedRecords on groupedRecords.someField = someTable.someField
where totalRecords.records > 0
然后您只需输出查询结果。看起来您知道如何在SQL中使用COUNT。。。你试过SUM吗?我想也许我看错了。我的第一反应总是在使用ColdFusion变量之前执行数学运算并修改查询本身中的数据,但这在这里并不谨慎。看看我下面的答案-我想它对你有用。如果你需要保存一个列表,把它放在一个数组中,以后再转换成一个列表。我想你是对的。抢手货我很累,只是想把逻辑拼凑起来。
<cfoutput>#listSum(myList)#</cfoutput>
with totalRecords as
(select count(*) records
from etc),
groupedRecords as
(select someField, count(*) recordsForField
from etc
group by someField)
select whatevever
, (groupedRecords.recordsForField / totalRecords.records) * 100 percentage
from someTables
join groupedRecords on groupedRecords.someField = someTable.someField
where totalRecords.records > 0