我正在使用MS SQL Server 2014和Coldfusion 10,并且正在我的桌面上发布问题。创建pdf时,我的桌面上某些用户的My Associate Total会被跳过。有没有人知道我做错了什么会让一些用户跳过Associate Total字段?
<cfset date1 = CREATEODBCDATETIME(form.StartDate & '00:00:00')>
<cfset date2 = CREATEODBCDATETIME(form.EndDate & '23:59:59')>
<!--- WHITELIST OF VALID LOCATIONS --->
<cfset validLocs = "OPERATIONS, CCC, QA, DS, PS, LWR, NR, SDL, FSC">
<cfset locList = "">
<cfif structKeyExists(FORM,"location") AND len(FORM.location) GT 0>
<cfloop list="#FORM.location#" index="vLoc">
<cfif listContainsNoCase(validLocs, vLoc) GT 0>
<cfset locList = listAppend(locList,vloc)>
</cfif>
</cfloop>
<cfelse>
</cfif>
<cfquery datasource="#application.dsn#" name="GetEmployeeInfo">
SELECT *
FROM (
SELECT t1.associate
, a.assoc_name
, t1.trans_location
, t1.checklistsByAssocLoc
, t1.totalChecklistsByAssoc
, CASE WHEN ( coalesce(t1.totalChecklistsByAssoc,0) > 0 ) THEN (CAST(t1.checklistsByAssocLoc AS decimal(8,2))/t1.totalChecklistsByAssoc) * 100 ELSE 0 END AS totalChecklistsByAssocLocPct /* This gives you a percent of associate location checklists over count of checklists by Associate */ , t1.totalChecklistsByLoc
, CASE WHEN ( coalesce(t1.totalChecklistsByLoc,0) > 0 ) THEN (CAST(t1.checklistsByAssocLoc AS decimal(8,2))/t1.totalChecklistsByLoc) * 100 ELSE 0 END AS totalChecklistsByLocPct /* This gives you a percent of associate location checklists over count of checklists by Location */
, ROW_NUMBER() OVER (PARTITION BY associate ORDER BY associate, trans_location) AS rnA
, ROW_NUMBER() OVER (PARTITION BY associate, trans_location ORDER BY trans_location) AS rnL
, t1.rnTotAssoc
FROM (
SELECT c.associate
, c.trans_location
, COUNT(*) OVER (PARTITION BY c.associate, c.trans_location) AS checklistsByAssocLoc /* Gives you a count of checklists by Associate and Location >> I think this is 1.1 locCntr */
, COUNT(*) OVER (PARTITION BY c.associate) AS totalChecklistsByAssoc /* Gives you a count of checklists by Associate */
, COUNT(*) OVER (PARTITION BY c.trans_location) AS totalChecklistsByLoc /* Gives you a count of checklists by Location */
, ROW_NUMBER() OVER (PARTITION BY c.associate ORDER BY c.trans_location DESC) AS rnTotAssoc
FROM cl_checklists c
WHERE c.[DATE] >= <cfqueryparam value="#date1#" cfsqltype="cf_sql_timestamp" />
AND c.[DATE] <= <cfqueryparam value="#date2#" cfsqltype="cf_sql_timestamp" />
AND c.trans_location IN ( <cfqueryparam value="#locList#" cfsqltype="cf_sql_varchar" list="true" /> )
) t1
INNER JOIN tco_associates a ON t1.associate = a.assoc_id
) s2
WHERE s2.rnA = 1 OR s2.rnL = 1
ORDER BY s2.assoc_name, s2.trans_location
</cfquery>
<cfif GetEmployeeInfo.recordcount is 0>
<td width="85%" valign="top" class="mainmodule"><p class="pageheader">CHECKLIST STATS REPORT</p><br />
No matching records found
</td>
</tr>
</table>
</body>
</html>
<cfelse>
<cfif FORM.Format IS "print">
<cfdocument format="pdf" scale="75" backgroundvisible="yes" overwrite="no" fontembed="yes">
<td width="85%" valign="top" class="mainmodule"><p class="pageheader">CHECKLIST STATS REPORT</p><br />
<style type="text/css">
table{
text-align:center;
}
</style>
<table>
<thead>
<th><strong>Associate Name</strong></th>
<th><strong>Location</strong></th>
<th><strong>Checklists Generated by Associate</strong></th>
<th><strong>Checklists Generated by Selected Location(s)</strong></th>
<th><strong>Associate Percentage of Location Total</strong></th>
</thead>
<tbody>
<cfoutput query="GetEmployeeInfo">
<tr>
<td><cfif rnA EQ 1><strong>#assoc_name#</strong></cfif></td>
<td><cfif rnL EQ 1>#trans_location#</cfif></td>
<td>#checklistsByAssocLoc#</td>
<td>#totalChecklistsByLoc#</td>
<td>#DecimalFormat(totalChecklistsByLocPct)# %</td>
</tr>
<cfif rnTotAssoc EQ 1>
<tr>
<td>Associate Total</td>
<td></td>
<td>#totalChecklistsByAssoc#</td>
<td>#totalChecklistsByLoc#</td>
<td>#DecimalFormat(totalChecklistsByLocPct)# %</td>
</tr>
</cfif>
</cfoutput>
</tbody>
</table>
</cfdocument>
</cfif>
</cfif>
结果应该如下:
Brierton, David SDL 1 32195 0.00 %
Associate Total 1 32195 0.00 %
有些显示为:
Brierton, David SDL 1 32195 0.00 %
答案 0 :(得分:1)
该查询看起来正确。
rnA = 1将给出包含关联名称的第一个实例的行。
rnL = 1将为该位置的第一个实例提供行。
rnTotAssoc = 1将给出一个关联的最后一行,包括他们的位置。下一行应显示关联记录的总数。每位员工应至少拥有1条rnTotAssoc = 1的记录。
http://sqlfiddle.com/#!6/56892/10
你有糟糕的数据会弄乱结果吗?尝试挑出一两个人,并提出他们应该正确的计数。您可以从查询中删除WHERE s2.rnA = 1 OR s2.rnL = 1
以查看进入这些计数的所有行。