Results 1 to 2 of 2
  1. #1
    forestpool is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2015
    Posts
    1

    How to write a query to return the average of three number (from the max value)

    Hi all, I have a table looks like this

    Date ValueX
    Jan 14 12
    Feb 14 18
    Mar 14 5
    Apr 14 4
    May 14 10
    Jun 14 5
    Jul 14 16


    I would like a query that returns the AVERAGE figure of the three values starting from the maximum value



    In this case, it is (18+5+4) / 3 = 9

    Thanks

  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,626
    Won't be simple.

    Need to pull TOP 3 date records starting with the max ValueX record. This requires nested subquery http://allenbrowne.com/subquery-01.html or domain aggregates.

    SELECT TOP 3 * FROM tablename WHERE [Date] >= DLookup("[Date]", "tablename", "ValueX=" & DMax("ValueX", "tablename")) ORDER BY [Date];

    This assumes the dates are unique.

    Now use that query in another query to average the 3 records.

    SELECT Avg([ValueX]) AS AvgVal FROM query1;

    Date is a reserved word. 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: 9
    Last Post: 08-19-2014, 12:41 PM
  2. Replies: 4
    Last Post: 10-26-2013, 02:04 PM
  3. Replies: 7
    Last Post: 08-03-2011, 08:00 AM
  4. Average value propagate on the number
    By besoqor in forum Queries
    Replies: 1
    Last Post: 05-24-2010, 05:11 PM
  5. Replies: 5
    Last Post: 10-08-2009, 05:15 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