Solved

AVG() or SUM() in different fields with nulls

Posted on 2000-05-15
4
347 Views
Last Modified: 2012-06-21
I have used Avg() to calculate the average of valus in a single field that may contain null values. Now I want to calculate the average of 5 values that are in different fields. It is possible that some of these values can be null and I will not want to include these in the average. In this case when I try to add the 5 values when one of them is null I come up with a null value. Can anyone help me wiht this? Thanks in advance.

Susan
0
Comment
Question by:susantrider
  • 2
4 Comments
 
LVL 2

Expert Comment

by:Simonac
ID: 2811590
u can use IsNull(FIELD) to include it or not in the AVG(), something like:

For each field in fields
   if not IsNull(field)
      SUM = SUM + field
      intNbrValues = intNbrValues + 1
   end if
next

  AVG = SUM/intNbrValues
0
 
LVL 10

Accepted Solution

by:
paasky earned 100 total points
ID: 2811628
Hello Susan

I think you need own function to do the calculation. Then use it with your query. The function could look like this (add this into your global Module):

Public Function MyAVG(field1, field2, field3, field4, field5) As Variant
Dim AvgCount As Byte
   
    ' assume all fields contain value
    AvgCount = 5
   
    ' minus 1 if field1..5 is null
    AvgCount = AvgCount + IsNull(field1)
    AvgCount = AvgCount + IsNull(field2)
    AvgCount = AvgCount + IsNull(field3)
    AvgCount = AvgCount + IsNull(field4)
    AvgCount = AvgCount + IsNull(field5)
   
    If AvgCount > 0 Then
        MyAVG = (Nz(field1, 0) + Nz(field2, 0) + Nz(field3, 0) + Nz(field4, 0) + Nz(field5, 0)) / AvgCount
    End If
   
End Function

Test results using Debug Window:
? MyAVG(null,null,null,2,4)
 3

? MyAVG(5,5,5,5,null)
 5

Usage example with query:

SELECT MyAVG(Number1, Number2, Number3, Number4, Number5) AS Average
FROM tblNumbers;

Hope this helps,
Paasky
Usage with query:
0
 

Author Comment

by:susantrider
ID: 2811722
Paasky, you have helped me agian. Thanks. The other answer was also helpful. Thank you!
0
 
LVL 10

Expert Comment

by:paasky
ID: 2811753
Always happy to help you Susan. :o)

Best regards,
Paasky
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now