473,789 Members | 2,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calculating Averages

rdscott
2 New Member
Access 2003

I have several fields in a table that I want to sum, get an average and update the avg value in another field. This is for each record. If anyone can help, what is the best way of doing this? I tried using the avg function, but it averages the whole column.
Jan 8 '07 #1
3 1961
Killer42
8,435 Recognized Expert Expert
I have several fields in a table that I want to sum, get an average and update the avg value in another field. This is for each record. If anyone can help, what is the best way of doing this? I tried using the avg function, but it averages the whole column.
At the simplest level, you could do an update query to update FieldX to ([Field1]+[Field2]+[Field3]/3).
Jan 8 '07 #2
rdscott
2 New Member
thanks, but how would I eliminate the fields that have zero values. e.g. in your example if field 3 has a zero value, i want just field 1 and field 2 values divided by 2
Jan 8 '07 #3
Killer42
8,435 Recognized Expert Expert
thanks, but how would I eliminate the fields that have zero values. e.g. in your example if field 3 has a zero value, i want just field 1 and field 2 values divided by 2
Perhaps it would be simplest to create a VB function which does the calculation, then use that function in your SQL.

For example (I forget how to write an UPDATE query)...
Expand|Select|Wrap|Line Numbers
  1. SELECT fAverageOf3([Fld1], [Fld2], [Fld3]) As FieldX FROM [TABLE];
Then in a VBA module, create a public function something like this (again, this is just off the top of my head)...
Expand|Select|Wrap|Line Numbers
  1. Public Function fAverageOf3(ByVal Val1 As Single, ByVal Val2 As Single, ByVal Val3 As Single) As Single
  2. Dim Count As Long
  3. Count = 0 - (Val1 <> 0) - (Val2 <> 0) - (Val3 <> 0)
  4. If Count Then
  5.   fAverageOf3 = (Val1 + Val2 + Val3) / Count
  6. End If
  7. End Function
Note that True is represented by -1, hence the subtractions in calculating Count.

I expect there are ways this approach could be made more generally applicable, rather than depending on a set type and number of fields (3 Single in this example).
Jan 8 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
9484
by: Stephen Miller | last post by:
Firstly, sorry for the long post, but I've included a fair bit of sample data. Im doing a comparision of 10yr Bond prices and CPI adjustments, with an 18 week moving average of the CPI. I'm using a nested sub-query to calculate the moving average, but I'm having difficulty selecting exactly 18 data points (ie When I include the 'HAVING COUNT(C1.Closes) = 18' line, I get no results). Can anyone help?
2
2534
by: Jeff Blee | last post by:
I am hoping someone can help me. I am making a Access 97 app for a person and have run up against a problem to do with MS Graph. There is a table that has a number of data elements and a date field and entries are entered each month. A graph is required that has three of the data elements represented basically on the y axis and time as months along the x axis. So for each month there is a group of three columns representing the data...
2
13608
by: Wayne Aprato | last post by:
I've read most, if not all, of the posts on moving average and still can't find a simple solution to my problem (if a simple solution exists!) I have a table with 2 fields: Hours and Injuries. I have a query based on this table which shows these 2 fields and calculates a third field: Frequency Rate, based on a formula which uses the Hours and Injuries fields. Is there a simple way of A: using yet another calculated field in the query...
1
2467
by: josecruz | last post by:
I have created a main report, which is controlled with a date range parameter from the main form. The report has several sub reports, which perform calculations (counts and averages at the subreport level) based on the date range from the main form. My question or problem is that I have to calculate a percentage based on the result of two of the subreports. I created a text box on the main report and I enterd the following to calculate the...
5
2017
by: ye2127 | last post by:
Hi, I have two fields in my report. One of them is school name(the school name appears multiple times). The other field is class test score. How would I go about calculating the test average for each school? I can calculate the average of all the schools but am having difficulty coding some kind of sort procedure for an average test score of all the classes in each school. Thanks...
1
1519
by: ye2127 | last post by:
Hi, I have two fields in my report. One of them is school name(the school name appears multiple times). The other field is class test score. How would I go about calculating the test average for each school?(In a report) I can calculate the average of all the schools but am having difficulty coding some kind of sort procedure for an average test score of all the classes in each school. Does someone know the code for such a procedure?...
3
2225
by: ken.ditto | last post by:
I have a database that stores data collected from a variety of instruments (pressure, temperature, flow rate, etc.) everytime there is a 1% change in the value. The Project Manager wants to be able to take the data and generate a report that shows a series of timed averages across a larger time span. For example, over an eight hour time range, he wants the instrument averages in five minute blocks. 00:00:01 - 00:05:00 Press1avg01 |...
1
3513
kmartinenko
by: kmartinenko | last post by:
I have a table with over 12,000 entries. I have created a form (with the help of this forum) that will return the search results based upon the stop and stop time selected. See post http://www.thescripts.com/forum/thread757933.html for additional context. Now, I want to take this a step further by querying this table by clicking a new command button on the form which will open up a new query displaying the average number for six of these...
5
2362
by: lildiapaz | last post by:
Hi, I have wrote two queries, one to find the average for one date, and the second query to find the average for another date. And I stored the results of the query to a local string object. Is there anyway to compare the two averages by getting the difference between the two. example query String myQuery = "" + "select name, age, date, avg(count) from product where date=interval -7 Day + '2001-08-13' " + " group by name ";
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9984
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.