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

%3CLINGO-SUB%20id%3D%22lingo-sub-2242856%22%20slang%3D%22en-US%22%3EHow%20to%20get%20a%20Weighted%20Average%20instead%20of%20an%20average%20of%20averages.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2242856%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%20all.%20I%20am%20wanting%20to%20get%20a%20weighted%20average%20instead%20of%20an%20average%20of%20averages.%20See%20my%20excel%20example%20attached.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHow%20would%20one%20go%20about%20getting%20a%20weighted%20average%20instead%20of%20an%20average%20of%20averages%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2242856%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAccess%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2252847%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20get%20a%20Weighted%20Average%20instead%20of%20an%20average%20of%20averages.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2252847%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F676026%22%20target%3D%22_blank%22%3E%40slohombre%3C%2FA%3E%26nbsp%3B%3A%20Like%20this%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20%0A%20%20%20%20Week%2C%20%0A%20%20%20%20Yield%2C%20%0A%20%20%20%20Acres%2C%20%0A%20%20%20%20%5BYield%5D%2F%5BAcres%5D%20AS%20YieldPerAcre%2C%20%0A%20%20%20%20(Select%20Sum(Acres)%20From%20Crop)%20AS%20TotalAcres%2C%20%0A%20%20%20%20(Select%20Sum(Yield)%20From%20Crop)%20AS%20TotalWeight%2C%20%0A%20%20%20%20%5BTotalWeight%5D%2F%5BTotalAcres%5D%20AS%20WeightedAverage%0AFROM%20%0A%20%20%20%20Crop%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EOutput%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22crop.png%22%20style%3D%22width%3A%20593px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F269374iCB67B0693165F706%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22crop.png%22%20alt%3D%22crop.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

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

@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:

crop.png

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;

@isladogs It's a question about formatting. You can have two decimals all over, if you prefer so:

 

crop2.png

@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