Question : Linking MS Tables to calculate average of field

I am using
- MSAccess
- Microsoft SQL Server  2000 - 8.00.760 (Intel X86)
- FrontPage 2002

I am rying to create a table to pull data from an MS Access database.

The fields I need to use are:
tblGeneral.ChartID
tblGeneral.Complexity
tblGeneral.HCF
tblStaff.ChartID
tblStaff.LibFTE

I have the first two columns working but am not sure how to get the average of column 3 (LibFTE) for each complexity level. I want to average the results of tblStaff.LibFTE for each complexity level for those records where tblGeneral.HCF=Yes. Each table has the ChartID as a connecting field. Table example:

Complexity          # of Medical              Average #
Level                      Facilities                  FTE
1a                               10                             3
1b                                 5                             2
1c                                22                            4
2                                  17                            6


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
<%
	dim title
	title = "Complexity Levels and Staffing"
%>


<% 
	'---Create recordset 

	Dim sql, objRS, s_name, s_order, s_next_order
	 
	sql = "SELECT Complexity, count(*) as ComplexityCount " & _ 
		"FROM tblGeneral " & _ 
         "WHERE AllHCF = 'YES' " & _
		"GROUP BY Complexity "
	
	s_name =  Request.querystring("sort")
	
	If Request.Querystring("order") = "1" Then
		s_order = "ASC"
		s_next_order = 0
	Else
		s_order = "DESC"
		s_next_order = 1
	End If
	
	Select Case s_name
		Case "Complexity"
			sql = sql + "ORDER BY Complexity " + s_order + " "
		Case "ComplexityCount"
			sql = sql + "ORDER BY Count(*) " + s_order + " "
	End Select

	Set objRS = Server.CreateObject("ADODB.Recordset") 
	objRS.Open sql, objConn 



'------------------
'Calculations for staff averages

	sqlAvgLibFTE = "SELECT avg(tblStaff.LibFTE) AS AvgLibFTE " & _
	"FROM tblStaff " 

	Set objAvgLibFTE = Server.CreateObject("ADODB.Recordset")
	objAvgLibFTE.Open sqlAvgLibFTE, objConn




%>


<html>

	<head>
		<title><%=title%></title>
		
		<link rel=stylesheet type="text/css" href="/CodeReuse/style.css">
	</head>

	<body>
	
		<h2 align="left"><font face="Arial" size="4"><%=title%></font></h2>
		</p>


		<%
		   	If (objRS.EOF) Then
		   		'---NO ROWS TO DISPLAY
		   	Else
		   		'---DISPLAY ROWS IN A HTML FORMATTED TABLE
		%>
			<table class="CCTable" cellspacing='0' border='1' cellpadding='5' width='400px>
				<TR style='background-color: white;'>
				<TH class="CCTable" scope='col' width='150px'><font size='1' color='blue'><a href='?sort=Complexity&order=<%=s_next_order%>'><font color='blue'><b>Complexity<br>Level</b></font></a></th>
				<TH class="CCTable" scope='col' width='150px'><font size='1' color='blue'><a href='?sort=ComplexityCount&order=<%=s_next_order%>'><b># of Medical<br>Centers</b></a></font></th>
				<TH class="CCTable" scope='col' width='150px'><font size='1' color='blue'><a href='?sort=ComplexityCount&order=<%=s_next_order%>'><b>Average # of<br>FTE</b></a></font></th></TR>
		<%                             
		
			Dim r_complexity, r_complexity_count
		         
		  	Do While (NOT objRS.EOF)
				r_complexity = Trim( objRS("Complexity") )
				r_complexity_count = Trim( objRS("ComplexityCount") )
				r_complexity_count = Trim( objRS("ComplexityCount") )
			                                         
		%>
				<tr>
					<td class="CCTable"><%=r_complexity%>&nbsp;</td>
					<td class="CCTable" align="center"><%=r_complexity_count%>&nbsp;</td>
					<td class="CCTable" align="center">&nbsp;</td>
				</tr>
		<%                                      
				'---MOVE TO NEXT ROW   
				objRS.MoveNext()
			Loop  
		%>
		        </table>
		<%                              
		 	End If
		       
		         
			'---CLEAN UP
			objRS.Close
			Set objRS= Nothing
		%>
	</body>
</html>

Answer : Linking MS Tables to calculate average of field

You can use the Avg() function.  Modify lines 12 and 13 like this:

sql = "SELECT Complexity, count(*) as ComplexityCount, " & _
        "Avg( LibFTE ) as AvgFTE FROM tblGeneral " & _  

--
Graham

Random Solutions  
 
programming4us programming4us