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

    Query to select values according to ID and date, then average

    Hello everybody -



    I'm new to using SQL, and am trying to create a query or VBA routine. I have a database of many measurements which are associated with a location (numeric value) and a date (julian days). For example -

    Location Date Measurement
    4000 6025 20
    4000 6025
    40
    4000 6050 70
    6000 6025 30

    There are some 90,000 of these measurements. As you notice, there are multiple locations and some locations have multiple measurements per year. What I'm trying to do is assemble a new table where there is only one measurement per year per location, with an average value taken if there is more than one measurement for that year at any particular location.

    So, the after running the query/routine, a new table would be spit out that looks like this:

    Location Date Measurement (Average)
    4000 6025 30
    4000 6050 70
    6000 6025 30

    So, essentially, if there's only 1 measurement at a location for a year, it would just be moved over to the new table unchanged. If there are 3 measurements at one location, but only two were from the same year, then the two would be averaged and brought over and the other would be moved over as a separate entry.

    Sorry that this is so specific. Maybe a For/Next statement in VBA would be appropriate? Thanks for any help

  2. #2
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    As far as I can see, a simple totals query should do what you want. Group by Location and Year, and average the measurement field (choose average as the aggregate function).
    BTW, Date is a bad name for a table. See this link http://allenbrowne.com/AppIssueBadWord.html

    Never mind; I see it's not that simple. Will dig deeper.

    Well, it helps to enter the data samples in a test table rather than the expected values . So it is that simple. Here's a sql for it (change to your table/field names):

    SELECT tblJulian.Location, tblJulian.Dte, Avg(tblJulian.Measurement) AS AvgOfMeasurement
    FROM tblJulian
    GROUP BY tblJulian.Location, tblJulian.Dte;
    Last edited by Micron; 01-24-2016 at 09:39 PM. Reason: reserved words
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    mlindaman1 is offline Novice
    Windows 10 Access 2013
    Join Date
    Jan 2016
    Posts
    3
    Awesome, worked perfectly!

    Thanks - I'll have to brush up on how GROUP BY statements work.

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

Similar Threads

  1. Replies: 22
    Last Post: 01-23-2015, 01:16 PM
  2. Replies: 7
    Last Post: 12-04-2013, 01:55 PM
  3. Replies: 1
    Last Post: 05-09-2013, 09:13 AM
  4. Query to show AVERAGE of all values in a field
    By taimysho0 in forum Queries
    Replies: 9
    Last Post: 01-09-2012, 11:18 AM
  5. Select query for Exponential Moving Average.
    By krishna79 in forum Queries
    Replies: 0
    Last Post: 01-22-2009, 05:52 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