涉及用户和位置的CF查询问题

时间:2016-04-06 20:46:45

标签: sql-server coldfusion group-by cfquery

我正在尝试找出正确设置查询的最佳方法。基本上我有一个迷你表格,要求location EmployeeName StartDateEndDate。我不幸地在两个我无法链接的服务器上使用两个MS SQL Server表。

我的目标是拥有5列左侧的姓名|位置名称|关联位置清单|总位置清单|关联百分比

我使用的主要表格是cl_checklists:我使用的三个列是date来检查表单上选定的日期,associate表示EmployeeNames,trans_location表示不同的地点。

    <cfset result = {} /> 
<cftry> 
    <cfset date1 = CREATEODBCDATETIME(form.StartDate & '00:00:00')>
    <cfset date2 = CREATEODBCDATETIME(form.EndDate & '23:59:59')>

    <cfquery datasource="#application.dsn#" name="GetEmployeeInfo">
        SELECT  *
        FROM    cl_checklists
        WHERE   date >=  <cfqueryparam value="#date1#" cfsqltype="cf_sql_timestamp" />
                AND date <= <cfqueryparam value="#date2#" cfsqltype="cf_sql_timestamp" />
                AND trans_location IN ( <cfqueryparam value="#FORM.location#" cfsqltype="cf_sql_varchar" list="true" />  )
                AND associate IN ( <cfqueryparam value="028,998,28" cfsqltype="cf_sql_varchar" list="true" />  )
    </cfquery>

     <cffunction name="getop_id" access="public" returntype="string"> 
        <cfargument name="associate"  > 
        <cfquery name="spitOutop_id" datasource="#application.userinfo_dsn#"> 
            SELECT assoc_name 
            FROM dbo.tco_associates 
            WHERE assoc_id= #arguments.associate# 
        </cfquery> 
        <cfreturn spitOutop_id.assoc_name > 
     </cffunction> 

<table border="1" id="Checklist_Stats">
    <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>
    <cfquery name="allAssociatesQry" dbtype="query">
        SELECT DISTINCT associate, COUNT(*) AS associateCount FROM GetEmployeeInfo GROUP BY associate ORDER BY associate 
    </cfquery>
      <cfloop query="allAssociatesQry">
        <cfset thisAssociateName = trim(allAssociatesQry.associate) />
      <cfquery name="allLocCodeForAssociateQry" dbtype="query">
          SELECT trans_location,count(trans_location) AS locCntr FROM GetEmployeeInfo WHERE associate='#thisAssociateName#' GROUP BY trans_location ORDER BY trans_location
      </cfquery>
        <cfoutput query="allLocCodeForAssociateQry">
          <tr>
              <td><strong>#thisAssociateName#</strong></td>
              <!---<td><strong>#getop_id(192)#</strong></td>--->
              <td>#allLocCodeForAssociateQry.trans_location#</td>
              <td>#allLocCodeForAssociateQry.locCntr#</td>
              <td>#allAssociatesQry.associateCount#</td>
              <td>#NumberFormat((allLocCodeForAssociateQry.locCntr/allAssociatesQry.associateCount) * 100, '9.99')#%</td>
          </tr>
          <cfset thisAssociateName = "" />
      </cfoutput>
      </cfloop>
      <tr>
        <td><strong>Total</strong></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
      </tr>
    </tbody>
</table>

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

enter image description here

我无法弄清楚如何更改第4列的查询,总计显示该位置的用户总检查清单,但我想要第4列中位置的总清单。有人请告诉我我哪里出错了,以及如何找到该位置的总数而不是该位置的用户总数,这已经是第三列了。

我可以在像add

之类的查询中查询
<cfquery datasource="#application.dsn#" name="GetLocationInfo">
        SELECT  *
        FROM    cl_checklists
        WHERE   date >=  <cfqueryparam value="#date1#" cfsqltype="cf_sql_timestamp" />
                AND date <= <cfqueryparam value="#date2#" cfsqltype="cf_sql_timestamp" />
                AND trans_location IN ( <cfqueryparam value="#FORM.location#" cfsqltype="cf_sql_varchar" list="true" />  )
    </cfquery>

然后在循环中添加另一个输出的总计数?

1 个答案:

答案 0 :(得分:2)

问题是您使用allAssociatesQry作为第4列&#39;由选定位置生成的核对清单,其按{{1}分组但是,您希望此处第4列中的总核对表计数在associate而不是Location上进行分组。

您可能需要考虑将它们分开!

保持代码不变,只需添加一个单独的查询来计算Associate查询后的第4列:

allLocCodeForAssociateQry

现在使用此<cfquery name="allLocCountForAssociateQry" dbtype="query"> SELECT trans_location,count(trans_location) AS totalLocCount FROM GetLocationInfo WHERE trans_location IS NOT NULL AND trans_location IN (#QuotedValueList(allLocCodeForAssociateQry.trans_location)#) GROUP BY trans_location </cfquery> cfoutput查询循环中此查询的过滤值,如下所示:

allLocCodeForAssociateQry

更新

David,基于我们对聊天的讨论,您提到要显示每个Associate组结果的总计,并且您希望在报告结尾处汇总结果。

基于此,我发布了一个解决方案 note :我相信在<cfoutput query="allLocCodeForAssociateQry"> <tr> <td><strong>#thisAssociateName#</strong></td> <!---<td><strong>#getop_id(192)#</strong></td>---> <td>#allLocCodeForAssociateQry.trans_location#</td> <td>#allLocCodeForAssociateQry.locCntr#</td> <!--- Change 4th column to: ---> <td>#allLocCountForAssociateQry['totalLocCount'][CurrentRow]#</td> <td>#NumberFormat((allLocCodeForAssociateQry.locCntr/allAssociatesQry.associateCount) * 100, '9.99')#%</td> </tr> <cfset thisAssociateName = "" /> </cfoutput> 中使用分组会产生更简化和最佳的结果,但对于时间的推移我选择使用当前的工作解决方案。

我还添加了大量评论,使其更具可读性和清晰度。

以下是解决方案:

cfloop