Results 1 to 10 of 10
  1. #1
    Ashe is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2010
    Location
    Blacksburg VA
    Posts
    65

    Weighted Average in SQL

    Hello,
    I'm trying to create a function in an access query that will create a weighted average. For groups of projects, there is a status and a related rank. For each group the basic idea would be:
    (project status)*(project rank)/(sum(project rank)

    I've tried the following sql code in query design, but I'm getting an aggregation error

    Code:
    WA: (([tbl_project].[status]*[tbl_targets].[RANK])/(Sum([tbl_targets].[RANK])))



    I had tried the code below, just an average of the projects and their ranks, but this isn't truly a weighted average, and it's certainly showing in my results.
    Code:
    WA: Avg([tbl_project.[status]*[tbl_targets].[RANK])
    Any suggestions of guidance would be greatly appreciated. Thank you!

  2. #2
    Ashe is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2010
    Location
    Blacksburg VA
    Posts
    65
    I should note the exact error is "You tried to execute a function that does not include the specified function '[tbl_project].[status]*[tbl_targets].[RANK])/(Sum([tbl_targets].[RANK]' as part of an aggregate functions.

    I'm hoping that this is because I'm not dividing correcting or something like that...

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,621
    In the Total row of the query select Expression.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    Ashe is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2010
    Location
    Blacksburg VA
    Posts
    65
    That's set in the query. Still getting the error... (thanks though!)

  5. #5
    Ashe is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2010
    Location
    Blacksburg VA
    Posts
    65
    Update:

    I found some threads online that indicate that using division in a query causes an error because it is not a function the query immediately recognizes (as it is not an option from the drop-down list in the "Totals" row). By typing the expression and setting the "Totals" row to "Group By" I was able to forgo the aggregation run-time error.

    I think there's still some explanation needed as to why division isn't an automatic option for Access queries, but since the queries working, I can't complain too much.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,621
    I never encountered problem with division in query expressions. Division is an operation just like +, -, *, not a function. You would get the same error message with any of the operators. Apparently the issue is with the divisor because it is not an aggregate calc. Guess I never tried one like that before.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Ashe is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2010
    Location
    Blacksburg VA
    Posts
    65
    You're right. I thought I had it figured out, but I had to average everything I wanted to divide with to make it an aggregate. I'm still not confident that I'm getting a weighted average at this point.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,621
    Here is a sample calc from my project: Sum([AdjPCI]*[Area])/Sum([Area]). Not my formula, I was told to use it and they call it a weighted average. It fits the example of this article http://www.meadinkent.co.uk/xlwtdavg.htm

    Check out Wiki's info http://en.wikipedia.org/wiki/Weighted_mean
    and this http://wiki.answers.com/Q/What_is_a_weighted_average
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    Ashe is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2010
    Location
    Blacksburg VA
    Posts
    65
    Yup- this is what I have too. Except for some reason, now it seems I need to squeeze more IIf qualifiers in.
    I have occassional blank(null) instances that are being calculated into the weighted average.
    I must not be applying these correctl- (as Access is rejecting them).
    I tried putting them in after the sum functions, but this didn't work. ( for example: /(sum(IIf(tbl_rank.rank)))). I also tried umbrellaing the IIf before the whole expression, but I guess this isn't the correct syntax either (judging by the error in expression message). Any suggestions?


    Thanks for all your help!

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,621
    Your IIf expression is incomplete.

    Here is example with IIF:
    Sum(IIf([Use]="Taxiway",[AdjPCI]*[Area],0))/Sum(IIf([Use]="Taxiway",[Area],0))

    I don't actually have data here now to check this formula for Null issue, not sure my data has any Nulls.

    Sum aggregate calc should ignore Nulls. Test that by doing a simple Sum on a field with Nulls.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Weighted-average inventory costing.
    By evander in forum Database Design
    Replies: 9
    Last Post: 01-03-2011, 08:32 AM
  2. Finding a weighted average
    By oldman in forum Queries
    Replies: 3
    Last Post: 09-10-2010, 08:41 PM
  3. Average help please
    By C90RanMan in forum Programming
    Replies: 1
    Last Post: 08-01-2010, 12:14 PM
  4. Replies: 13
    Last Post: 05-28-2010, 11:57 AM
  5. Weighted Average - Challenges
    By edmund_xue in forum Access
    Replies: 0
    Last Post: 04-02-2008, 12:54 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums