Results 1 to 5 of 5
  1. #1
    wishkey is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2013
    Posts
    3

    query with inner join that takes average of related records

    I have two tables [Events] and [Weather]. The event table contains two relevant fields [EventName], [EventDateTime]. The weather table contains [DateTime] and [Temperature].

    I want to create a query that displays all events and the weather information for that time. Since events last three hours, I want to take an average of time x, time x+1 and time x+2, where time x = [EventDateTime]. I know how to create an inner join to display the weather at the start time of each event, just don't know how to take average of that and the two following times.

    any help would be appreciated. thx

  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
    This would be done with an aggregate (Totals) GROUP BY query or in a report using Grouping & Sorting features with aggregate calcs in footer.

    You only want 3 records averaged? Are there only 3 events each date?

    How is the weather relevant?
    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
    wishkey is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2013
    Posts
    3
    Events occur irregularly. From Monday-Thursday there are none and then on weekend there are 2-4 each day. Each event lasts three hours (hence average of three hourly weather observations).

    Weather observations are hourly (168 per week).

    Basically I would like to find average weather conditions for each event, so that I can observe the relationship between weather and attendance. (does hot weather deter crowds at concerts? Does rain? How much? Given temperature x and rainfall chance y, what crowd can i expect at a given event).

    I will test aggregate query and get back to you. Thx

  4. #4
    wishkey is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2013
    Posts
    3
    I still can't figure out how to make it average the three weather observations related to event. If event starts at 2pm, i want to average 2pm, 3pm and 4pm and ignore the other values for the day. Any ideas?

    I've attached example database

    Cheers
    Attached Files Attached Files

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Try:

    Query1
    SELECT [EventID] & "-" & [ID] AS EventWeatherID, Events.EventID, Events.EventName, Events.EventDate, Events.EventDateTime, Weather.ID, Weather.Date1, Weather.DateTime, Weather.[Air Temperature in degrees C]
    FROM Weather INNER JOIN Events ON Weather.Date1 = Events.EventDate;

    Query2
    SELECT Query1.* FROM Query1 WHERE Query1.EventWeatherID IN
    (SELECT TOP 3 EventWeatherID FROM Query1 AS Dupe WHERE Dupe.EventID = Query1.EventID ORDER BY Dupe.DateTime DESC, Dupe.EventID)
    ORDER BY Query1.EventID;

    Query3
    SELECT Query2.EventID, Query2.EventName, Query2.EventDate, Avg(Query2.[Air Temperature in degrees C]) AS [AvgOfAir Temperature in degrees C]
    FROM Query2
    GROUP BY Query2.EventID, Query2.EventName, Query2.EventDate;

    Or create report with Query2 as the RecordSource and use report Grouping & Sorting features with aggregate calcs in group footer.
    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. Adding records to a join table using a query
    By brharrii in forum Queries
    Replies: 5
    Last Post: 04-12-2013, 11:08 AM
  2. Replies: 2
    Last Post: 10-12-2012, 02:33 PM
  3. Replies: 6
    Last Post: 07-23-2011, 12:35 PM
  4. query won't display records with no related records
    By James Brazill in forum Queries
    Replies: 1
    Last Post: 06-28-2011, 02:10 AM
  5. Access Takes my Query and Alters it!
    By dso808 in forum Queries
    Replies: 2
    Last Post: 10-01-2010, 03:45 PM

Tags for this Thread

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