 # How to get a Weighted Average instead of an average of averages.

Hi, all. I am wanting to get a weighted average instead of an average of averages. See my excel example attached.

How would one go about getting a weighted average instead of an average of averages?

Thanks!

4 Replies

# Re: How to get a Weighted Average instead of an average of averages.

@slohombre : Like this:

``````SELECT
Week,
Yield,
Acres,
[Yield]/[Acres] AS YieldPerAcre,
(Select Sum(Acres) From Crop) AS TotalAcres,
(Select Sum(Yield) From Crop) AS TotalWeight,
[TotalWeight]/[TotalAcres] AS WeightedAverage
FROM
Crop;``````

Output: # Re: How to get a Weighted Average instead of an average of averages.

Many thanks to both @slohombre and @Gustav Brock for providing such clear explanations.

One point possibly worth mentioning.

It would be better to display the total weight(32) & total acres (19.5 not 20) as e.g Double numbers rather than integer.

The weighted average is correctly calculated as 32/19.5=1.641 rather than 32/20=1.6.

However, when doing aggregate queries, Access may not correctly calculate expressions based on other calculated expressions. Sometimes you will need to run the query twice for the correct results

I would therefore recommend either calculating the weighted average as a separate query or using the actual expressions for both Total Weight and Total Acres in the Weighted Average expression.

``````SELECT
Week,
Yield,
Acres,
[Yield]/[Acres] AS YieldPerAcre,
Sum(Acres) AS TotalAcres,
Sum(Yield) AS TotalWeight,
Sum(Yield)/Sum(Acres) AS WeightedAv
FROM Crop;``````

# Re: How to get a Weighted Average instead of an average of averages.

@isladogs It's a question about formatting. You can have two decimals all over, if you prefer so: # Re: How to get a Weighted Average instead of an average of averages.

@Gustav Brock
Yes I realised that but hopefully you can see why I mentioned it.
However, my main point was about reusing calculated expressions in another field in the sql