简体   繁体   中英

Last entry of a history table with MS-Access

I developed a database with Access with the next architecture:

First table:

  1. Identifier
  2. Serial number
  3. Revision (Yes/No)

And the second table, linked to the first one by their identifiers, that it's being used like a history, keeping some events with his responsible:

  1. Identifier
  2. Date event
  3. Responsible event
  4. Event description

So, I'm trying to make a SQL query that brings me the last event in the history table for a determined identifier by this way:

SELECT M.ID, M.SN, Last(H.Date) AS MaxDate, Last(H.Responsible) AS LastResponsible, 
Last(H.Description) AS LastDescription
FROM TABLE1 AS M INNER JOIN History AS H ON M.ID=H.ID
WHERE M.Disponible=No
GROUP BY M.ID, M.SN;

But it's bringing to me sometimes values in history that are not the last ones, why could be the reason?

You need to first group History to find the Max entries. Make a subquery of this, and then JOIN the subquery with TABLE1 .

See this question for a good example:
Is it better / more efficient to use sub queries or SELECT statements within the WHERE clause (in MS Access)
(you want the "combined as a single query" variant).

If you can't get it to work, add a comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM