Results 1 to 8 of 8
  1. #1
    Sck is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2010
    Posts
    82

    query help - just need max record for specific field

    so I have a table structure as follows:

    Employee Task YYYYMM Hours
    1 1 201903 3
    1 2 201903 5
    1 2 201905 8
    2 2 201904 5
    2 2 201905 6

    I need the results to be Max YYYYMM for each Employee/task as follows

    RESULT:

    Employee Task YYYYMM Hours
    1 1 201903 3
    1 2 201905 8
    2 2 201905 6

    I don't know if I am just having a brain cramp or what but I cant figure out the sql to make it happen. any help is GREATLY appreciated




    sorry about the data spacing. Not how I typed it... data should align with headers.

  2. #2
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    Create a query and add employee, task and date fields.
    In the design ribbon click the totals button.
    Each field will now have Group By in the Totals row.
    Change that to Max for the Date field.
    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

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    (using isladogs idea)

    select EmpID, Max(Date) from table

  4. #4
    Sck is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2010
    Posts
    82
    Thanks i was including the hours which was causing it to pull all rows figured it was something "simple"

  5. #5
    Join Date
    Apr 2017
    Posts
    1,673
    You have to create a saved query which returns latest date for every employee
    (from ranman's post, with some corrections)
    Code:
    qLastDate = select Employee, Max(Date) As LastDate from table GROUP BY Employee
    After that you can use your original table and saved query as linked in new query
    Code:
    SELECT tbl.* FROM qLastDate ld INNER JOIN Table tbl ON tbl.Employee = ld.Employee AND tbl.Date = ld.LastDate

  6. #6
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    The OP wants the task field as well which is why I included it in post #2

    Code:
    Select Employee, Task, Max(Date) As MaxDate from TableName GROUP BY Employee, Task
    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

  7. #7
    Join Date
    Apr 2017
    Posts
    1,673
    Quote Originally Posted by isladogs View Post
    The OP wants the task field as well which is why I included it in post #2
    Missed this part. But this doesn't change much - only adds a 3rd Join condition into final query

  8. #8
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    Yes I agree - assuming the OP really needs the hours but his answer in post #4 suggests not.
    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

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

Similar Threads

  1. Replies: 2
    Last Post: 11-14-2017, 09:05 AM
  2. Replies: 3
    Last Post: 11-28-2016, 03:17 PM
  3. Replies: 4
    Last Post: 05-18-2016, 03:32 PM
  4. Replies: 2
    Last Post: 01-14-2015, 12:00 PM
  5. Replies: 1
    Last Post: 08-01-2012, 12:50 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