Sorry, I misunderstood your structure. I was assuming a normalized table
structure.
Are the values of Comu_1 to 3 ever Null (blank)? Your code assumes that
they are never blank
I would use a custom VBA function to do this. It will make life a lot
easier if you continue with this structure and someone decides you need
to
add Comu_4 and Comu_5 to the table. If you can you might consider
redesigning the table structure and moving Comu to a related table.
I've attached a function that you can paste into a module (make sure the
module does NOT have the same name as the function).
You can then call this function to get the average.
fGetMeanAverage(IIF(Comu_1=88888 or
Comu_1=99999,Null,Comu_1),IIF(Comu_2=88888 or
Comu_2=99999,Null,Comu_2),IIF(Comu_3=88888 or Comu_3=99999,Null,Comu_3))
as
ComuAvg
SELECT tblColonyCounts.ColonyCountID
, tblColonyCounts.Colony,
tblColonyCounts.Area
, tblColonyCounts.COMU_1
, tblColonyCounts.COMU_2
, tblColonyCounts.COMU_3
, fGetMeanAverage(IIF(Comu_1=88888 or
Comu_1=99999,Null,Comu_1),IIF(Comu_2=88888 or
Comu_2=99999,Null,Comu_2),IIF(Comu_3=88888 or Comu_3=99999,Null,Comu_3))
as
ComuAvg
FROM tblColonyCounts
ORDER BY tblColonyCounts.ColonyCountID;
'============ Code Follows =================
Public Function fGetMeanAverage(ParamArray Values())
'John Spencer UMBC CHPDM
'Last Update: April 5, 2000
'Calculates the arithmetic average (mean) of a group of values passed to
it.
'Sample call: myAvg = GetMeanAverage("1","TEST","2", "3",4,5,6,0)
' returns 3 (21/7)
'Ignores values that cannot be treated as numbers.
Dim i As Integer, intElementCount As Integer, dblSum As Double
intElementCount = 0
dblSum = 0
For i = LBound(Values) To UBound(Values)
If IsNumeric(Values(i)) Then 'Ignore Non-numeric values
dblSum = dblSum + Values(i)
intElementCount = intElementCount + 1
End If
Next i
If intElementCount > 0 Then
'At least one number in the group of values
fGetMeanAverage = dblSum / intElementCount
Else
fGetMeanAverage = Null 'No number in the group of values
End If
End Function
PeterKappes said:
Hi John,
I don't think this will work, since AVG is an aggregate function. This
may
be reinventing the wheel, but this is what I came up with. Do you see
a
way
I could streamline the SQL statement?
SELECT tblColonyCounts.ColonyCountID, tblColonyCounts.Colony,
tblColonyCounts.Area, tblColonyCounts.COMU_1, tblColonyCounts.COMU_2,
tblColonyCounts.COMU_3, IIf([comu_1]=99999 Or [comu_1]=88888=True,0,1)
AS
Value1, IIf([comu_2]=99999 Or [comu_2]=88888=True,0,1) AS Value2,
IIf([comu_3]=99999 Or [comu_3]=88888=True,0,1) AS Value3,
IIf(([value1]+[value2]+[value3])=0=True,[comu_1],(([comu_1]*[value1])+([comu_2]*[value2])+([comu_3]*[value3]))/([value1]+[value2]+[value3]))
AS COMUAvg
FROM tblColonyCounts
ORDER BY tblColonyCounts.ColonyCountID;
:
AVG(IIF([Your Field] = 88888 or [Yourfield] = 99999, Null,
[YourField])
as
TheAverage
Hey,
I have three columns of numeric data that I need to get an average
from.
However, some of the numeric data actually isn't "values" but place
holders
for null data. (e.g. 88888 = no data collected and 99999 = unknown
value).
Obviously, if I simply average the three columns then I get
incorrect
averages wherever there was no data collected or unknown values.
I'm
not
sure if I can put if statements into my query or how or if I need to
call
a
function (which I also don't know how to do). How would I be able
to
get
this information averaged such that 88888 and 99999 "values" are not
used
in
the calculation, but are entered into the results if that was the
only
"value" recorded for the three columns. There is never a combo of
88888
and
99999. Here is an example:
7, 6, 88888 = 6.5
99999, 99999, 99999 = 99999
5, 88888, 88888 = 5
10, 20, 30 = 20
Thanks in advance,
Peter