Results 1 to 5 of 5
  1. #1
    Russell is offline Novice
    Windows 10 Office 365
    Join Date
    Jan 2023
    Posts
    7

    Query to Select most recent date for each unit.

    Im sure this is easy for someone, but SQL not my thing and been struggling for too long to get the syntax right to get this to work.



    Table: tblDutyList
    Fields:
    DutyID (Key)
    DutyStation
    DutyDate

    Table collects multiple entries for all Stations, never more than one on a given date.

    I need a query to give me the most recent date from each station.

    I can get DISTINCT Stations, and I can sort by date - but cant get those things working together.

    Thanks.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,923
    If you only want DutyStation and DutyDate info, that is simple.

    SELECT DutyStation, Max(DutyDate) AS DD FROM tblDutyList GROUP BY DutyStation;

    Let the query designer get the syntax for you and you don't really need to know SQL. Study how to build aggregate queries. https://www.teachucomp.com/aggregate...0that%20order.

    If you want the entire record that has the latest date, that gets complicated.
    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
    Join Date
    Apr 2017
    Posts
    1,679
    To get entire records for all stations is not very complicated, but you can't use designer for this - you have to enter the query manually. Something like
    Code:
    SELECT tl.*
    FROM tblDutyList tl INNER JOIN (SELECT DutyStation, Max(DutyDate) AS DutyDate FROM tblDutyList GROUP BY DutyStation) LastDuty ON LastDuty.DutyStation = tl.DutyStation AND LastDuty.DutyDate = tl.DutyDate

  4. #4
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    To get the latest record per group you either need a 2 step process (stacked query) or a subquery (as shown in post #3)

    For more details on this type of subquery, see http://allenbrowne.com/subquery-01.html#TopN
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,923
    Can use query designer to build 'stacked query'. The 2 steps are:

    1. build and save inner aggregate query

    2. build query that joins table and query objects

    After building with designer, switch to SQL View to see and learn syntax.
    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. Most recent date query
    By Pbear88 in forum Queries
    Replies: 10
    Last Post: 01-29-2019, 03:50 PM
  2. Query for most recent date
    By benjamin.m.winchester in forum Queries
    Replies: 3
    Last Post: 12-22-2015, 01:33 PM
  3. Replies: 6
    Last Post: 06-09-2015, 09:51 AM
  4. Replies: 3
    Last Post: 03-11-2014, 07:32 PM
  5. Most recent Date query
    By SmartestIdiot in forum Queries
    Replies: 1
    Last Post: 01-11-2014, 07:56 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