Results 1 to 5 of 5
  1. #1
    Bdowns is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Feb 2012
    Posts
    28

    Query records where date/time field value is close or equal

    Table A has a Date/Time field and a field with a bunch of measurements. Table B has a Date/Time field and bunch of other miscellaneous fields. I want to create a query that includes the measurement values from Table A where the Date/Time field is closest to that of Table B, but not greater. Also, sometimes the values are equal and I want those measurement values too. Any ideas?

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    how close is 'close' and do you always only want the most recent record ONLY or do you want all the records within, say, 5 minutes, of a specified date/time?

    Is your query intended to return only one value?

    For instance if I put in 1/1/2013 12:00:00 PM (noon)

    and I have records at:

    1/1/2013 12:00:00 PM
    1/1/2013 11:59:59 AM
    1/1/2013 11:59:29 AM


    Do you *always* only want to return the top value or do you want to show all records in the, say, 30 seconds, prior to noon which would include 2 of these records.

  3. #3
    Bdowns is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Feb 2012
    Posts
    28
    Thanks, those are important questions. For now I am only trying to return one value (the most recent one), but I may benefit from being able to return one value as an average of a few recent records or all falling after the previous record in the table. It would be great to know how to return both an average of recent records in a fixed time interval as well as an average of all the records after the previous record's Date/Time. Any help is greatly appreciated. Thanks!

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Assuming your records can never have the same time stamp returning the record that has the most recent record is pretty easy. Let's say you have a table (tblTest) with two fields (CUSTOMERID and DATETIMESTAMP)

    In order to return the most recent record what you'd do is

    SELECT TOP 1 * FROM tblTest WHERE CUSTOMERID = x AND DATETIMESTAMP <= y ORDER BY DATETIMESTAMP DESC

    Where x would be the PK you're searching for
    and y would be the date/time stamp you're trying to get close to

  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,624
    Here is more about the TOP N parameter http://allenbrowne.com/subquery-01.html#TopN
    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: 01-24-2013, 10:10 AM
  2. Date/Time Field Query...
    By Coffee in forum Queries
    Replies: 5
    Last Post: 07-05-2011, 11:27 PM
  3. Replies: 7
    Last Post: 05-31-2011, 11:51 AM
  4. Greater than or equal to date
    By stryder09 in forum Access
    Replies: 1
    Last Post: 04-14-2011, 03:54 PM
  5. Running a Delete (Records) Query on Close
    By NoiCe in forum Queries
    Replies: 1
    Last Post: 07-12-2009, 06:17 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