Null values are disregarded by the avg function. So if a record doesn't have a value that record will not be included to calculate the average. That is correct imo, but it may not be what you want. In that case you can divide the sum by the count, i.e. sum(units)/count(units). Or you can try to replace the nulls in the script with real values.