gpt4 book ai didi

sql - 按字母顺序排列的名称然后按字母顺序排列的位置 "Order By"不起作用

转载 作者:行者123 更新时间:2023-12-02 01:09:51 26 4
gpt4 key购买 nike

我很困惑。我以为我是将同事姓名按升序排列,然后将我的位置作为子类别升序排列。但即使是我的同事姓名也没有按字母顺序排列。

<cfset date1 = CREATEODBCDATETIME(form.StartDate & '00:00:00')>
<cfset date2 = CREATEODBCDATETIME(form.EndDate & '23:59:59')>

<cfquery datasource="#application.dsn#" name="GetEmployeeInfo">
SELECT trans_location, date, associate
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="#FORM.EmployeeName#" cfsqltype="cf_sql_varchar" list="true" /> )
</cfquery>

<cfquery datasource="#application.dsn#" name="GetLocationInfo">
SELECT trans_location, date, associate
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>

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

<cfquery name="allAssociatesQry" dbtype="query">
SELECT DISTINCT associate, COUNT(*) AS associateCount FROM GetEmployeeInfo GROUP BY associate ORDER BY associate
</cfquery>

<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>
<!--- aggregate variables --->
<cfset aggrAssociateChecklist = 0>
<cfset aggrLocationChecklist = 0>

<cfloop query="allAssociatesQry">
<!--- get Associate's name --->
<cfset thisAssociateCode = trim(allAssociatesQry.associate)>
<cfset thisAssociateName = getop_id(thisAssociateCode) />
<!--- 1.1 get all trans_location code and total counts for the current Associate --->
<cfquery name="allLocCodeForAssociateQry" dbtype="query">
SELECT trans_location,count(trans_location) AS locCntr FROM GetEmployeeInfo WHERE associate='#thisAssociateCode#' GROUP BY trans_location ORDER BY trans_location
</cfquery>
<!--- 1.2 get the aggregate of checklist count generated by the current Associate for each location --->
<cfquery name="qTotalChecklistCountForAssociate" dbtype="query">
SELECT SUM(locCntr) AS totalAssocChecklist FROM allLocCodeForAssociateQry
</cfquery>

<!--- 2.1 get the total location checklist for each location available for the current Associate --->
<cfquery name="allLocChecklistForAssociateQry" dbtype="query">
SELECT trans_location,count(trans_location) AS totalLocCount FROM GetLocationInfo WHERE trans_location IN (#QuotedValueList(allLocCodeForAssociateQry.trans_location)#) GROUP BY trans_location ORDER BY trans_location
</cfquery>
<!--- 2.2 get the aggregate of location checklist generated by the current Associate --->
<cfquery name="qTotalLocChecklistForAssociate" dbtype="query">
SELECT SUM(totalLocCount) AS totalLocChecklist FROM allLocChecklistForAssociateQry
</cfquery>
<!--- display record for the current Associate --->
<cfoutput query="allLocCodeForAssociateQry">
<tr>
<!---<td><strong>#thisAssociateCode#</strong></td>--->
<td><strong>#thisAssociateName#</strong></td>
<td>#allLocCodeForAssociateQry.trans_location#</td>
<td>#allLocCodeForAssociateQry.locCntr#</td>
<td>#allLocChecklistForAssociateQry['totalLocCount'][CurrentRow]#</td>
<td>#NumberFormat((allLocCodeForAssociateQry.locCntr/allLocChecklistForAssociateQry['totalLocCount'][CurrentRow]) * 100, '9.99')#%</td>
</tr>
<cfset thisAssociateName = "" />
</cfoutput>
<!--- 3.1 get sub total for each Associate group --->
<cfset totalAssocChecklist = qTotalChecklistCountForAssociate.totalAssocChecklist>
<cfset totalLocChecklist = qTotalLocChecklistForAssociate.totalLocChecklist>
<!--- 3.2 add to the aggregate --->
<cfset aggrAssociateChecklist += totalAssocChecklist>
<cfset aggrLocationChecklist += totalLocChecklist>
<!--- display sub total for each Associate group --->
<cfoutput>
<tr>
<td>Associate Total</td>
<td></td>
<td>#totalAssocChecklist#</td>
<td>#totalLocChecklist#</td>
<td>#NumberFormat((totalAssocChecklist/totalLocChecklist) * 100, '9.99')#%</td>
</tr>
</cfoutput>
</cfloop>
</tbody>
</table>

有人能告诉我为什么这不按字母顺序列出员工姓名,然后在员工姓名按顺序排列后按顺序列出地点吗?

我以为 ORDER BY associate 正在这样做,但名字完全分散了。任何帮助将不胜感激。

最佳答案

这应该会按照您想要的顺序为您提供 Associates。

<cfquery datasource="#application.dsn#" name="GetEmployeeInfo">
SELECT ltrim(rtrim(c.associate)) AS thisAssociateCode --<< GetEmployeeInfo
, a.assoc_name AS thisAssociateName --<< getop_id.assoc_name
, c.trans_location --<< GetEmployeeInfo
, c.[date] --<< GetEmployeeInfo
FROM cl_checklists c
INNER JOIN dbo.tco_associates a ON c.associate = a.assoc_id
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="#FORM.location#" cfsqltype="cf_sql_varchar" list="true" /> )
AND c.associate IN ( <cfqueryparam value="#FORM.EmployeeName#" cfsqltype="cf_sql_varchar" list="true" /> )
ORDER BY a.assoc_name
</cfquery>

它还允许您删除 getop_id 函数。

你可以摆脱:

<cfset thisAssociateCode = trim(allAssociatesQry.associate)>
<cfset thisAssociateName = getop_id(thisAssociateCode) />

关于sql - 按字母顺序排列的名称然后按字母顺序排列的位置 "Order By"不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45555353/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com