In doing some database work I ran across a strange problem. I’m trying to use the built-in average function in Access for a certain population. When I run the query I get a different answer than when I look up the individual persons and then average the values. Anyone notice anything in the SQL below that would lend itself to giving two different answers?
using avg function:
SELECT DISTINCT tblKentuckyCounties.Appalachian, tblKentuckyCounties.Metro, Avg(Visits.admitNumUlcers) AS AvgOfadmitNumUlcers
FROM (tblKentuckyCounties INNER JOIN Person ON tblKentuckyCounties.ID = Person.County) INNER JOIN Visits ON Person.PersonID = Visits.PersonID
GROUP BY tblKentuckyCounties.Appalachian, tblKentuckyCounties.Metro
HAVING (((tblKentuckyCounties.Appalachian)=No) AND ((tblKentuckyCounties.Metro)=No))
;
looking up records individually:
SELECT DISTINCT tblKentuckyCounties.Appalachian, tblKentuckyCounties.Metro, Visits.admitNumUlcers, Person.PersonID
FROM (tblKentuckyCounties INNER JOIN Person ON tblKentuckyCounties.ID = Person.County) INNER JOIN Visits ON Person.PersonID = Visits.PersonID
GROUP BY tblKentuckyCounties.Appalachian, tblKentuckyCounties.Metro, Visits.admitNumUlcers, Person.PersonID
HAVING (((tblKentuckyCounties.Appalachian)=No) AND ((tblKentuckyCounties.Metro)=No));
One possible explanation for the difference you’re seeing is this GROUP BY in the second query:
Any rows with duplicates of those 4 fields would be rolled up into a single group. Check by changing the field list for the second query:
If rows_in_group is > 1, you have at least part of the answer as to why the averages don’t agree.