我是使用coldfusion的新手,我试图弄清楚如何输出平均值的最大数量。现在我可以找到特定部门的平均值,但是我如何从该数据中获取该列表中的最大数量并显示最大平均数?
这是我的代码
<tr>
<td>#csedept_name#</td>
<td><div align="right">#totalcount#</div></td>
<td><div align="right"><cfif totalstars eq ''>–<cfelse>#totalstars#</cfif></div></td>
<td><div align="right"><cfif totalstarsgiven eq ''>–<cfelse>#totalstarsgiven#</cfif></div></td>
<td><div align="right"><cfif totalstars eq ''>–<cfelse><cfset avgstars = totalstars / totalcount>#DecimalFormat(avgstars)#</cfif></div></td>
</tr>
<cfset countEmployees = countEmployees + totalcount>
<cfif totalstarsgiven neq ''><cfset countStarsGiven = countStarsGiven + totalstarsgiven></cfif>
<cfif totalstars neq ''>
<cfset countStarsReceived = countStarsReceived + totalstars>
<cfset countStarAverage = countStarAverage + avgstars>
</cfif>
.....
<th><div align="right">#countEmployees#</div></th>
<th><div align="right">#countStarsReceived#</div></th>
<th><div align="right">#countStarsGiven#</div></th>
<th><div align="right">#avgstars#</div></th> //here to display the max of the average
以下是查询代码:
<h1><cfoutput>#self#</cfoutput></h1>
<cfquery datasource="Intranet" name="GetDepartments">
SELECT * FROM CSEReduxDepts
</cfquery>
<cfquery datasource="Intranet" name="GroupStars">
SELECT execoffice_status, employeedept, COUNT(*) as 'totalstars'
FROM CSEReduxResponses
WHERE execoffice_status = 1
GROUP BY execoffice_status, employeedept
</cfquery>
<cfquery dbtype="query" name="GetTotalStars">
SELECT *
FROM GroupStars, GetDepartments
WHERE GroupStars.employeedept = GetDepartments.csedept_id
</cfquery>
<cfif GetTotalStars.RecordCount gt 0>
<cfquery datasource="PhoneList" name="GetAllData">
SELECT dept.csedept_id, COUNT(*) as 'totalcount'
FROM employee, dept
WHERE employee.dept_id = dept.dept_id
AND employee.emp_status = 1
AND dept.csedept_id is not null
GROUP BY dept.csedept_id
</cfquery>
<cfquery dbtype="query" name="GetDepartmentTotalEmployeesCount">
SELECT *
FROM GetAllData, GetDepartments
WHERE GetAllData.csedept_id = GetDepartments.csedept_id
</cfquery>
<cfquery name="joinQuery" dbtype="query" >
SELECT *
FROM GetTotalStars
WHERE GetTotalStars.csedept_id = -1
</cfquery>
<cfset QueryAddRow(joinQuery)>
<cfquery name="GetUnion" dbtype="query" >
SELECT *
FROM GetDepartmentTotalEmployeesCount, GetTotalStars
WHERE GetDepartmentTotalEmployeesCount.csedept_id = GetTotalStars.csedept_id
UNION
SELECT GetDepartmentTotalEmployeesCount.*, joinQuery.*
FROM GetDepartmentTotalEmployeesCount, joinQuery
WHERE GetDepartmentTotalEmployeesCount.csedept_id NOT IN (#ValueList(GetTotalStars.csedept_id)#)
</cfquery>
<cfquery datasource="Intranet" name="GroupStarsGiven">
SELECT execoffice_status, submitterdept, COUNT(*) as 'totalstarsgiven'
FROM CSEReduxResponses
WHERE execoffice_status = 1
GROUP BY execoffice_status, submitterdept
</cfquery>
<cfquery dbtype="query" name="GetTotalStarsGiven">
SELECT *
FROM GroupStarsGiven, GetDepartments
WHERE GroupStarsGiven.submitterdept = GetDepartments.csedept_id
</cfquery>
<cfquery name="joinQuery2" dbtype="query" >
SELECT *
FROM GetTotalStarsGiven
WHERE GetTotalStarsGiven.csedept_id = -1
</cfquery>
<cfset QueryAddRow(joinQuery2)>
<cfquery name="GetUnion2" dbtype="query" >
SELECT *
FROM GetUnion, GetTotalStarsGiven
WHERE GetUnion.csedept_id = GetTotalStarsGiven.csedept_id
UNION
SELECT GetUnion.*, joinQuery2.*
FROM GetUnion, joinQuery2
WHERE GetUnion.csedept_id NOT IN (#ValueList(GetTotalStarsGiven.csedept_id)#)
ORDER BY csedept_name ASC
</cfquery>
答案 0 :(得分:2)
一般来说,你可以用sql做到这一点。一般的想法是
select dept, max(number) maxnumber, avg(number) avgnumber
from department join
(select deptid, number
from department
where clause
) temp on department.deptid = temp.deptid
where clause
group by dept
确保两个where子句相同。
答案 1 :(得分:1)
有两种方法可以给这只猫上皮。如果你喜欢猫,我道歉。
首先,你可以像Dan建议的那样去做。很简单,对吧?
其次,我不推荐它超过另一个。
<cfset maxNum = ArrayMax(query['columnname']) />
但这是一个选项