Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 15th, 2003, 07:05 AM
Registered User
 
Join Date: Aug 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default find the average value from a column of values

For this code I wanted to find the average of many numerical values in a column (CategoryA) in a table. First I found the number of record/values in CategoryA which is held in the variable iRecordCount. Then I made an array to gather all the values in the column which is held in ARR1(). Then I had a series of if then statements that found the right number of records/values in ColumnA this would ensure that the correct number of array values were added together. Everything in the code is functional however, I plan to have hundreds of values in my table and many more columns so I would have to use hundreds and hundreds of if then statements. If anyone is out there who knows an easier and more efficient way of calculating the averages of values in a table I would greatly appreciate some help. I am sure this is a common problem but unfortunately I have not seen any tutorials or forum topics devoted to it.


'Find the number of values
Dim iRecordCount
iRecordCount = 0
DO WHILE NOT rsVariable.EOF
iRecordCount = iRecordCount + 1
rsVariable.MoveNext
Loop

'Find and hold the values in CategoryA
rsVariable.MoveFirst
DIM ARR1()
FOR I = 1 to iRecordCount
Redim preserve ARR1(I)
ARR1(I) = rsVariable("CategoryA")
rsVariable.MoveNext
NEXT

'Depending on the number of records add that number of records together using the values held in the array
if iRecordCount = 2 then
total2 = int(ARR1(1)) + int(ARR1(2))
response.write int(total2)/int(iRecordCount)
else
end if

if iRecordCount = 3 then
total3 = int(ARR1(1)) + int(ARR1(2)) + int(ARR1(3))
response.write int(total3)/int(iRecordCount)
else
end if
 
Old August 15th, 2003, 07:31 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jonax
Default

Code:
dim t, iTotal
For t = 1 to iRecordCount
    iTotal = iTotal + cInt(ARR(t))
Next
As for how you get your recordcount and fill your array, you should have a look at the GetRows method of the Recordset object.
http://msdn.microsoft.com/library/en...thod_oledb.asp

 
Old August 15th, 2003, 07:37 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

How about as bit of SQL instead?
Set rs = connectionName.Execute("SELECT AVG(ColumnA) AS TheAverage FROM TableName")
...
Response.Write rs("TheAverage")





Similar Threads
Thread Thread Starter Forum Replies Last Post
find the FIRST USED row/column? crmpicco Excel VBA 3 July 23rd, 2013 12:52 PM
identity column id find!! keyvanjan Classic ASP Basics 7 January 20th, 2007 01:12 PM
find the last used column crmpicco Excel VBA 2 May 20th, 2005 08:14 AM
Find column in datagrid Lesviper VB How-To 0 March 9th, 2005 07:54 AM
Find average for values stored in an array ja8261 VB.NET 2002/2003 Basics 2 October 29th, 2004 10:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.