Well your data is part of the problem.
First thing would be to save the Date and Time in one field. It is easy to
split out just the date or just the time. If you have both in one field you
can use an index on the field to speed up the search.
Another thing you can do is use two queries.
QueryOne: Get the latest date and time for each K1 value. Save the query as
QLatest. If you are able to limit the date range then add that to your query
with a WHERE clause and index the date field.
SELECT K1,
Max(CDate([Date] + [Time])) as LastDate
FROM Table1
GROUP BY K1
Again this would be much faster if you had the date and time in one field and
indexed. Also add an index on the K1 field. It should speed up the join
between table 1 and the query.
SELECT T1.K1, T1.K2, T1.K3
FROM (Table1 as T1
INNER JOIN Table2 as T2
ON T1.K1 Like "*" & T2.K1 & "*")
INNER JOIN qLatest as Q
ON T1.K1 = Q.K1
AND CDate(T1.[Date] + T1.[Time]) = Q.LastDate
Beyond that I can't think of any way to make the query faster. UNLESS the
time is not significant - you don't have more than one entry for K1 on any one
date. Then you could drop the time from the query. And you would index the
date field.
Good luck
John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
this code works fine for small tables. With some relatively large tables it
takes time or hangs up.
John said:
So now you want to return the record with the maximum of the Date and Time for
the match.
One method uses a correlated sub-query that would look something like the
following.
SELECT T1.K1, T1.K2, T1.K3
FROM Table1 as T1
INNER JOIN Table2 as T2
ON T1.K1 Like "*" & T2.K1 & "*"
WHERE CDate(T1.[Date] + T1.[Time)) =
(SELECT Max(CDate(Temp.[Date] + Temp.[Time)))
FROM T1 as Temp
WHERE Temp.K1 = T1.K1)
John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
[quoted text clipped - 14 lines]
xbcd 3 dt2 tm2
yrff 6 dt2 tm2