KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Calculating Averages with NULL Values
PRODUCT: 4D | VERSION: 13.0 | PLATFORM: Mac & Win
Published On: April 30, 2012

When creating a new field, the property "Map NULL values to blank values" is selected by default as shown:



When this property is enabled, 4D will process "blank" values and NULL values the same, they will be converted to the default value of the data type the field is associated with.

This becomes a problem when developers want to find the average value of a field that can contain NULL values. For example, there can be a form that asks for users age and N/A is an acceptable answer. By default all Real, Integer and Long Integer data types are 0. The average would not be accurate if all NULL values are converted to the default 0 value. To avoid this issue, the field in which the average value is desired should not have the property "Map NULL values to blank values" checked.

If an Integer field has values 100, 100, 100 and NULL, the following code would have $average result in 75 if the "Map NULL values to blank values" property is checked and 100 if the property is not checked:

C_REAL($average)

Begin SQL
 SELECT AVG(Field_1)
 FROM Table_1
 INTO :$average

End SQL