Results 1 to 10 of 10
  1. #1
    DBA-Meister is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    22

    Use max value from a table in a query from the same table.

    I have the following simple query:

    SELECT Last3MtchQry.[Thrower-Name], Int(Avg([Score-Avg])+0.5) AS Current3WkAvg, 672 - Int(Avg([Score-Avg])+0.5) AS CurrentHndyKap, Last3MtchQry.ID
    FROM Last3MtchQry
    GROUP BY Last3MtchQry.[Thrower-Name], Last3MtchQry.ID;



    Every week I have to manually change the hard-coded 672 value. I want to change the hard-coded 672 to the max average value so that the person with that value ends up with a zero handicap. Anyone got any ideas how to do this?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,623
    The average of what? Last week/month/season games, top 3? Is this average already calculated somewhere?
    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.

  3. #3
    DBA-Meister is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    22
    A separate query calculates the average score for each player for each week. The query used here queries that query to calculate the average of those three averages. For each player, this value is subtracted from the value for the player with the highest average. The difference is then used as the current week handicap.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,623
    You want to know the highest 3-week average by a single player? Then each player's 3-week average is subtracted from that one player's average to provide each player with a handicap?

    The ID of the high player is irrelevant, just need the highest average?

    Build the 3-week average query without the handicap calc.

    Build another query.

    SELECT [Thrower-Name], DMax("Average", "3weekAvgQry") - [Average] AS CurrentHndyKap FROM 3weekAvgQry;
    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.

  5. #5
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,610
    Not sure this is what you want, but you could try replacing the 672 with something like DMax("[Score-Avg]","[ Last3MtchQry]") which would give you :
    SELECT Last3MtchQry.[Thrower-Name], Int(Avg([Score-Avg])+0.5) AS Current3WkAvg, DMax("[Score-Avg]","[ Last3MtchQry]")
    - Int(Avg([Score-Avg])+0.5) AS CurrentHndyKap, Last3MtchQry.ID
    FROM Last3MtchQry
    GROUP BY Last3MtchQry.[Thrower-Name], Last3MtchQry.ID;
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  6. #6
    DBA-Meister is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    22
    When I use your code, I get this: "The Microsoft Access database engine cannot find the input table or query 'Last3MtchQry'. Make sure it exists and that it's name is spelled correctly."

    The query DOES exist and it is spelled correctly. Not sure what's going on here.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,623
    Did you use the query builder Design View to construct?
    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.

  8. #8
    DBA-Meister is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    22
    I used Create/Query Design, then went to "SQL" view and pasted the sql you suggested.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,623
    Reread my post 4. Did you build 2 queries as suggested?
    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.

  10. #10
    DBA-Meister is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    22
    I created new query MaxAvgScoreQry as follows:
    SELECT max(t1.Current3WkAvg) AS MaxAvgScore
    FROM Current3WkAvgQry AS t1;


    Then I inserted "(Select MaxAvgScore from MaxAvgScoreQry)" in place of the hardcoded numerical value. Success. Thx for the help.

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

Similar Threads

  1. Replies: 2
    Last Post: 10-09-2012, 03:20 PM
  2. Replies: 6
    Last Post: 05-10-2012, 08:20 PM
  3. Replies: 2
    Last Post: 12-20-2011, 07:33 AM
  4. Replies: 3
    Last Post: 08-16-2011, 02:20 PM
  5. Replies: 2
    Last Post: 10-27-2009, 07:09 AM

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