Results 1 to 10 of 10
  1. #1
    Sweet16 is offline Novice
    Windows XP Access 2003
    Join Date
    Sep 2011
    Posts
    9

    Get latest record

    I have 2 tables tbl_Rating_D and tbl_Company_Info.

    Tbl_Company_Info has the following fields: autoID, name, address, contact person, phone number

    Tbl_Rating_D has the following fields: autoID, agencyID (looked up from the name field in the Company Info table), rating, and date.

    Companies in the tbl_Company_Info are rated continuously. I would like to create a query that only shows the most recent rating (designated by the date) for each company but am having some difficulty achieving this.

  2. #2
    Stingaway is offline Efficiency Junkie
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2011
    Location
    The deep south. Keep going until you hit water basically.
    Posts
    224
    Changed a few field names around to avoid reserved names and for expediency, but the general idea would be something like this:

    SELECT tbl_company_info.agencyname, Max(tbl_Rating_D.ratingdate) AS MaxOfratingdate
    FROM tbl_company_info INNER JOIN tbl_Rating_D ON tbl_company_info.ID = tbl_Rating_D.agencyid
    GROUP BY tbl_company_info.agencyname;

    Hope it helps.

    -stingaway

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Couple ways to do this. Can use DMax() function. Create a field in query with expression: DMax("[date]","Tbl_Rating_D","agencyID=" & [agencyID]). Then set criteria for this field as [date].

    The other is a nested subquery that uses Max() function to return the Max date for each agencyID and then filter for the records where [date] matches the Max date.

    Stingaway, that query will return the Max date but will not return the record to show the rating.
    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.

  4. #4
    Sweet16 is offline Novice
    Windows XP Access 2003
    Join Date
    Sep 2011
    Posts
    9
    Thanks for both of the suggestions!!

    Do I insert the above expression in the criteria field of the date column?

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Go ahead and try. If it doesn't work then do what I suggested. I think either should work.
    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.

  6. #6
    Sweet16 is offline Novice
    Windows XP Access 2003
    Join Date
    Sep 2011
    Posts
    9
    June 7 you were correct. The query only returns the agency name and a date. I tried to insert the DMAX critiera into a field but I keep getting an error message stating I am missing and operator.

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Is agencyID a text field? If so, need apostrophe delimiters. Like:
    DMax("[date]","Tbl_Rating_D","agencyID='" & [agencyID] & "'")

    Post your exact SQL statement for analysis.
    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.

  8. #8
    Sweet16 is offline Novice
    Windows XP Access 2003
    Join Date
    Sep 2011
    Posts
    9
    Below is the query I inserted. This one gives me all of the records, including the older ones.

    SELECT tbl_Rating_D.AgencyID, tbl_Rating_D.Rating, tbl_Rating_D.Date, DMax("[date]","Tbl_Rating_D","AgencyID='" & [AgencyID] & "'") AS Expr1
    FROM tbl_Rating_D;

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Everything returns because you have not given filter criteria for the date:
    SELECT tbl_Rating_D.AgencyID, tbl_Rating_D.Rating, tbl_Rating_D.Date FROM tbl_Rating_D WHERE DMax("[date]","Tbl_Rating_D","AgencyID='" & [AgencyID] & "'") = [date];
    Last edited by June7; 09-23-2011 at 03:00 PM.
    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.

  10. #10
    Sweet16 is offline Novice
    Windows XP Access 2003
    Join Date
    Sep 2011
    Posts
    9
    Thanks for the feedback!!! A million thank yous!

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

Similar Threads

  1. Code to display latest record number field created?
    By rowardHoark in forum Programming
    Replies: 1
    Last Post: 01-31-2011, 08:03 AM
  2. How do you get the latest date?
    By radicrains in forum Queries
    Replies: 6
    Last Post: 10-27-2010, 08:59 PM
  3. Replies: 18
    Last Post: 08-22-2010, 05:22 AM
  4. Query latest record
    By asherbear in forum Queries
    Replies: 5
    Last Post: 08-02-2010, 03:58 PM
  5. Latest Date Records
    By Rick West in forum Queries
    Replies: 1
    Last Post: 09-25-2009, 11:16 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