473,473 Members | 1,484 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Average expression that can handle null values

5 New Member
Hi, I am trying to create a make-table query that includes an expression which will average four fields.

My problem is that the fields contain null values for certain rows.

I want the expression to take the average of the non-null values (including zeros) within the four fields and disregard the nulls.

Here's a table describing what I want my outcome table to show:

Expand|Select|Wrap|Line Numbers
  1. ID  Field1   Field2    Field3   Field4  Average
  2. 1    19         2                  5      8.67
  3. 2               3         2        1       2 
  4. 3     1         1         1        0      0.75
Here is the expression I am trying to use but something is messing it up.

Expand|Select|Wrap|Line Numbers
  1. Average:([Field1]+[Field2]+[Field3]+[Field4])/(4-iif(isnull([Field1]),1,0)-iif(isnull([Field2]),1,0)-iif(isnull([Field3]),1,0)-iif(isnull([Field4]),1,0))
Could someone help me write the appropriate expression?

Also, I would like my expression to return values with decimals to the tenths place.

Thank you! Gerry
Nov 11 '13 #1
6 3681
zmbd
5,501 Recognized Expert Moderator Expert
It looks like you are ignoring the Null fields in the average calculation, correct:(19+2+5)/3 not (19+2+0+5)/4?

-

I also suggest that you avoid using "Average" as a field name:
Access 2007 reserved words and symbols
AllenBrowne- Problem names and reserved words in Access
Nov 11 '13 #2
NeoPa
32,556 Recognized Expert Moderator MVP
If any of the fields in your expression hold Nulls then the dividend (The number which is divided by the divisor) becomes Null. As such, unless all fields are populated, the result will be Null.

Try instead :
Expand|Select|Wrap|Line Numbers
  1. AvgVal: Round(Nz([Field1],0)
  2.              +Nz([Field2],0)
  3.              +Nz([Field3],0)
  4.              +Nz([Field4],0))
  5.              /
  6.              (IIf([Field1] Is Null,0,1)
  7.              +IIf([Field2] Is Null,0,1)
  8.              +IIf([Field3] Is Null,0,1)
  9.              +IIf([Field4] Is Null,0,1),1)
Of course, this doesn't handle the "Divide by zero" error if ever all 4 fields are Null.

PS. Thanks to MorganaJ for the tip about rounding which I'd forgotten to include in the answer.
Nov 11 '13 #3
gaf44
5 New Member
NeoPa,

Thanks that works perfectly!

Gerry
Nov 11 '13 #4
NeoPa
32,556 Recognized Expert Moderator MVP
Pleased to help Gerry.

Also pleased that many others can find the answer to similar questions now this is here.
Nov 11 '13 #5
morganaj
12 New Member
Also to solve your rounding issue you can use, which rounds to two places:

Expand|Select|Wrap|Line Numbers
  1. Expression Name: Round(([Value1]+[Value2]),2)
Whatever you put as a value of AnyNumber is the decimal places to round to.

Expand|Select|Wrap|Line Numbers
  1. Round(NumbersOrExpression,AnyNumber)
Jan 7 '14 #6
NeoPa
32,556 Recognized Expert Moderator MVP
Indeed Morgana. I forgot to deal with that part of the question when I posted that originally. Since your post I've changed the post retrospectively to include the full answer as it should have been. Thank you for that.
Jan 8 '14 #7

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

Similar topics

3
by: John Smith | last post by:
Hello all: Does anyone happen to know the best way to handle null values for DateTimePickers? We have quite a few columns in our db that will not necissarily have a date for certain entries. ...
1
by: DKode | last post by:
I find myself writing repetitive functions for handling null values from my DB like so: Private Function SetDateNull(ByVal p_date As Object) As Date If (TypeOf (p_date) Is System.DBNull) Then...
11
by: eddy de boer | last post by:
Hello, in my aspx page I have the followong code: <asp:Repeater id="Repeater1" runat="server"> <ItemTemplate> .... <%# Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst"))...
0
by: Jason Coyne | last post by:
Here is another programming blog entry. Sorry for the normal friends :) I recently ran into a problem with a web service I was trying to call from c# where the web service returned null dates...
3
by: psycho | last post by:
how should i hanle null values returned by the datareader i am using OleDbDataReader for accessing access database.
10
by: Toby Gallier | last post by:
Hello! I have a form that is calculating averages as follows: " =(NZ()+Nz()+Nz())/3 " However I need to now adjust for null values , so for example if value2 is null I would then need to...
2
by: jehugaleahsa | last post by:
Hello: Is there a way to handle null values for ints, DateTime, etc. in generated DataTables? It seems it is not something the Microsoft crew thought of. That is hard for me to believe. ...
5
by: tonialbrown | last post by:
I have some code that updates a record's fields based on a selection from a list box (lstDelFrom). The user selects the record from the list box & it copies it into the fields , , etc. My...
1
by: apache626 | last post by:
I really need help with this one. I am using a script task to write data pulled from a MS SQL DB to a fixed width file. The script takes the ItemArray and writes the value of the column to the file...
1
by: NareshN | last post by:
Hi, I am using this stored procedure with pivot.If i dont have data i am getting null with this stored procedure.Can u tell me how to handle null.below query is pivot. I am using like...
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
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...
1
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...
1
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.