Results 1 to 5 of 5
  1. #1
    mdex is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2012
    Posts
    11

    Query Help - Return latest entry for distinct rows

    I have a table that includes an ID (key), TeamName, AccountType and DateChecked fields, amongst a few others.



    What I would like to be able to do is return the latest entry for each distinct TeamName/AccountType combination. I've tried to follow the example here as shown below but get an error that "Specified field 'TeamName' could refer to more than one table listed in the FROM clause of your SQL statement."

    Select TeamName, AccountType, ID
    from Checks a inner join
    (select TeamName, MAX(ID) as maxID
    from checks group by TeamName) b
    on a.TeamName=b.TeamName and a.ID=b.ID

    I've tried switching ID and DateChecked within the query tna. I'm already way past my knowledge of Access/SQL and as always am trying to bastardise code where I can. Any help is gratefully received.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Make Q1 to get MAX(Date),team,acct.
    make Q2 using Q1 and data table to get all the fields you wish to see.

  3. #3
    mdex is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2012
    Posts
    11
    Thanks for ths pointer.

    I'm struggling to get my head around the logic of this so please bear with me.

    I have Q1 to get the latest entry for each combination of TeamName/AccountType

    SELECT Checks.TeamName, Checks.AccountType, MAX(DateChecked) as max_date
    FROM Checks
    GROUP BY Checks.TeamName, Checks.AccountType;
    What I can't figure out is how to create Q2 to show the rest of the fields to accompany those found in Q1. I've tried reading up on joins but I've not managed to hit on a syntax that will run, nevermind give me the outcome I desire.

  4. #4
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    In query design view use Q1 as if it were a table. have that on there with the other table you wish to link to.

    drag the ID so it has a relationship then double click the fields you want.

    Thats if im not misunderstanding you.

  5. #5
    mdex is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2012
    Posts
    11
    Quote Originally Posted by Homegrownandy View Post
    In query design view use Q1 as if it were a table. have that on there with the other table you wish to link to.

    drag the ID so it has a relationship then double click the fields you want.

    Thats if im not misunderstanding you.
    That's exactly it. Simple now you mention it.

    Thanks

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

Similar Threads

  1. Replies: 3
    Last Post: 10-30-2016, 05:50 AM
  2. Replies: 3
    Last Post: 09-06-2014, 01:25 PM
  3. Query Expression to return only the latest data
    By Brinleigh217 in forum Queries
    Replies: 6
    Last Post: 03-23-2012, 02:53 PM
  4. Replies: 6
    Last Post: 07-21-2011, 04:51 AM
  5. Replies: 11
    Last Post: 09-02-2010, 01:59 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