Hi Anthony, and welcome to Bytes! Your logic does work - but only if an assumption is correct, which in this case it clearly isn't. The assumption is that the
entries for each client will always be put into the table in date order. The last autonumber FinRevID for each client is otherwise not reflecting the last review date. It is this which is causing the apparent ordering problems for your query. The use of Last on the FinRevID is the problem here, as it is changing the grouping within the query if the entry order does not follow strict date order.
I have come to this conclusion after testing your query on sample data; your query works as expected when all entries follow in date order, but fails should a review be entered out of sequence.
To resolve this we need to break the problem down into two parts: find the most recent review date for each client, then retrieve the FinRevID that corresponds to that date (recognising that it is not necessarily the highest-value ID for that client).
Enter the query below and save it as qryLastDate:
- SELECT [Banks Databse].[Client Number], Max([Banks Databse].[Last Updated]) AS [Last Review]
-
FROM [Banks Databse]
-
GROUP BY [Banks Databse].[Client Number];
Then join qryLastDate to your [Banks Databse] table in the following query to retrieve the FinRevID value corresponding:
- SELECT [Banks Databse].[Client Number], [Banks Databse].[Last Updated], [Banks Databse].FinRevID, DateDiff("d",[Last Updated],Date()) AS Days
-
FROM [Banks Databse] INNER JOIN qryLastDate ON ([Banks Databse].[Last Updated] = qryLastDate.[Last Review]) AND ([Banks Databse].[Client Number] = qryLastDate.[Client Number])
-
WHERE (((DateDiff("d",[Last Updated],Date()))<400));
Test data:
- Client Number Last Updated FinRevID
-
101 ..........06/02/2007 .....1
-
101 ..........06/02/2008 .....2
-
101 ..........06/06/2008 .....3
-
102 ..........03/06/2006 .....4
-
102 ..........03/09/2007 .....5
-
102 ..........03/05/2008 .....6
-
103 ..........01/05/2006 .....7
-
103 ..........01/08/2007 .....8
-
103 ..........01/09/2007 .....9
-
103 ..........01/05/2008 ....10
-
101 ..........08/04/2008 ....11
-
102 ..........02/03/2008 ....12
-
103 ..........01/04/2008 ....13
qryLastDate:
- Client Number Last Review
-
101 ..........06/06/2008
-
102 ..........03/05/2008
-
103 ..........01/05/2008
Final query result:
- Client Number Last Updated FinRevID Days
-
101 ..........06/06/2008 .....3 .....27
-
102 ..........03/05/2008 .....6 .....61
-
103 ..........01/05/2008 ....10 .....63
-Stewart