473,412 Members | 2,067 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Calculating Averages

rdscott
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 1944
Killer42
8,435 Expert 8TB
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
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 Expert 8TB
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
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...
2
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...
2
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...
1
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...
5
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...
1
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...
3
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...
1
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...
5
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.