Results 1 to 5 of 5
  1. #1
    knpaddac is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2017
    Posts
    22

    Expression to return MAX VALUES

    I have a database of school information with the following field names:


    county, school, year, enrollment

    I want to be able to create a comparison of each school to the largest school in their county for each year, so I want to run a query that will return
    1. school, 2. county, 3. enrollment, and 4. largest enrollment of any school in that county.

    How would I put that 4th item in a query?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Could use DMax() domain aggregate.

    Or build a report with grouping on county and use Max() function in a textbox in group footer. Report allows display of detail data as well as aggregate calcs.
    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
    knpaddac is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2017
    Posts
    22
    Quote Originally Posted by June7 View Post
    Could use DMax() domain aggregate.
    Ive looked this up and it seems like it should work, however I am having trouble figuring out how to state the criteria portion. There are 800+ schools spread over 92 different counties and I want each line in the returned data to include the largest enrollment number for that school's particular county.

    Currently, what I have looks like this:
    DMax("[Enrollment]","Year","[County]")

    This just returns the same value for every school and that value is the largest of all schools in the entire state. I know that it needs to be "[County]=SOMETHING" but I do not know what that SOMETHING needs to be to come back how I am wanting it.

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Create a separate totals query, include country (using GROUP BY) and enrollment (using MAX in the group by field). This will return one row/one value per country. Bring this query in to your original query, link by country.
    Last edited by aytee111; 07-28-2017 at 07:15 AM. Reason: fixed it!

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    As aytee111 offered, an aggregate query joined to the data is another approach and probably perform faster than DMax(). If you want the Max for each Year for each County, group on both fields and then compound link on both County and Year fields.

    Also, the syntax for DMax() would be:

    DMax("[Enrollment]", "tablename", "[County]='" & [County] & "' AND [Year]=" & [Year])


    BTW, year is a reserved word and should not use reserved words as names.
    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. Replies: 4
    Last Post: 06-26-2017, 12:19 PM
  2. Replies: 3
    Last Post: 10-30-2016, 05:50 AM
  3. Replies: 3
    Last Post: 03-27-2014, 08:11 AM
  4. Replies: 1
    Last Post: 06-27-2013, 09:33 AM
  5. Query Expression to return only the latest data
    By Brinleigh217 in forum Queries
    Replies: 6
    Last Post: 03-23-2012, 02:53 PM

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