Results 1 to 3 of 3
  1. #1
    mlindaman1 is offline Novice
    Windows 10 Access 2013
    Join Date
    Jan 2016
    Posts
    3

    select an entry within a group that is closest to the group's average


    Hello - I have another question that is related to my last. I again have a table that has entries that include a location value, a measurement value, and a date value.

    Location Date Measurement
    6050 56731 30
    6050 56731 50
    6050 56731 150
    5090 34700 20
    5090 34700 50

    This time, I need to create a query that will group the measurements by the Date and Location value, then select the entry within each group that is closest to the average. In the event that two or more measurements on the same date have the same absolute difference from the mean (such as when there are two values only), I need the query to select either one (it doesn't matter).

    The following SQL is the closest I've come (ignore the use of restricted words in this example). It doesn't work for several reasons, namely the GROUP BY clause doesn't allow for the subquery in the aggregate function.

    SELECT Table1.Location, Table1.Date, ABS(Table1.levVa - (SELECT AVG(Table1.levVa) FROM Table1)) AS AvgDevia
    FROM Table1
    GROUP BY Table1.Location, Table1.Date
    ORDER BY AvgDevia ASC
    LIMIT 1;

    If anybody has any ideas here, it'd be very appreciated - been stuck on this all day.

  2. #2
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    You will probably have to do this in two steps: sort by location and date, get the average of the location/date grouping, then use the subquery approach in another query where this first query is the source.
    that is closest to the average
    I think you'll also have to decide if the closest number is to be greater than or lesser than the average and use either choice in the criteria, or else introduce another step. Maybe a query that has two calculated fields to get the Min value that is greater than the Avg and the Max value that is less than the Avg. Those values would be the criteria in the last query.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    NTC is offline VIP
    Windows 7 64bit Access 2013
    Join Date
    Nov 2009
    Posts
    2,392
    It is not clear to me as to whether you have used the Aggregate Query feature in the product (in query design view is sigma symbol looks like a big E). This should give you what you seek in that Average is one of the choices for a column. Use the default Group for the other 2 columns.

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

Similar Threads

  1. Replies: 6
    Last Post: 06-09-2015, 09:51 AM
  2. Replies: 5
    Last Post: 01-04-2014, 02:29 PM
  3. GROUP by yet SELECT fields not within GROUP BY
    By johnseito in forum Access
    Replies: 25
    Last Post: 11-03-2013, 10:20 PM
  4. Replies: 0
    Last Post: 02-25-2013, 04:43 PM
  5. How do I group & average at the same time?
    By B20Pete in forum Queries
    Replies: 1
    Last Post: 07-07-2009, 12: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