Results 1 to 4 of 4
  1. #1
    thebigthing313 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    119

    How to write specific query efficiently

    So I'm kind of stumped on how to properly write this query.



    I have three tables involved:

    tblInspectionSites - 0:M - tblInspections - 0:M tblTreatments

    I am trying to write a query to report the details of the latest treatment(s) for each inspection site (if any). "Latest" is determined by a timestamp found in tblInspections.

    I am trying to accomplish at least finding a way to get the latest inspection which would make getting the latest treatment details easy.

    I am able to get the latest date of inspection using a MAX aggregate on the Timestamp, but how do I get the primary key(s) of those inspections with that latest date?

    Thanks in advance,
    Adrian

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,642
    A TOP N nested query might get you what you want http://allenbrowne.com/subquery-01.html#TopN.

    Another technique involves domain aggregate functions, which can be slow performers in query.
    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
    NTC is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2009
    Posts
    2,392
    make an Aggregate Query of tblInspections grouping on the Site Key field (or whatever you call it) and MAX on the date stamp; save that as 'LastInspectQ'

    then make a Select Query; with tblInspections and LastInspectQ joining the 2 fields both Site Key field and the TimeStamps fields; this query will result in just the tblInspections records that match - add all the fields you want. Save this as 'LastInspectQ2'....

    then use this 'LastInspectQ2' as the basis for finalizing what ever other new query involving the other tables is needed for your report.

  4. #4
    thebigthing313 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    119
    Thank you both for the responses, after some last minute guesses before the week ended on Friday, I stumbled upon NTC's solution on my own. But thank you anyway June7, I still found that post informative.

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

Similar Threads

  1. Replies: 1
    Last Post: 06-09-2013, 04:50 AM
  2. Replies: 1
    Last Post: 06-15-2012, 05:51 PM
  3. Help to write query?
    By mlandis in forum Queries
    Replies: 4
    Last Post: 02-17-2012, 09:42 AM
  4. Retrieving PDF's Efficiently
    By tcheck in forum Access
    Replies: 3
    Last Post: 08-05-2011, 07:30 AM
  5. write a query how to???
    By flamingo in forum Queries
    Replies: 0
    Last Post: 01-13-2009, 03:57 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