So to trouble shoot let's break this down into steps.
Step one: Does the following return any data? Is it correct?
SELECT YourTable.BuildingNumber, Max(YourTable.Date) as LatestDate
FROM YourTable
GROUP BY BuildingNumber
Step Two: Does the following return any data? And if so are the
Autonumbers
valid ones for what you are trying to get?
SELECT YourTable.AutoNumber
FROM YourTable INNER JOIN
(SELECT YourTable.BuildingNumber, Max(YourTable.Date) as
LatestDate
FROM YourTable
GROUP BY BuildingNumber) as Temp
ON YourTable.BuildingNumber = Temp.BuildingNumber
AND YourTable.[Date] = Temp.LatestDate)
Step Three: IF the above worked, then this should work unless things have
gotten too complex.
SELECT X.*
FROM YOURTable as X
WHERE X.AutoNumber IN
(SELECT YourTable.AutoNumber
FROM YourTable INNER JOIN
(SELECT YourTable.BuildingNumber, Max(YourTable.Date) as
LatestDate
FROM YourTable
GROUP BY BuildingNumber) as Temp
ON YourTable.BuildingNumber = Temp.BuildingNumber
AND YourTable.[Date] = Temp.LatestDate)
If the query in step 2 works, but step 3 fails then try saving the query
in
step 2 and using that in the where clause of step 3
SELECT *
FROM YourTable
WHERE YourTable.Autonumber =
(SELECT Autonumber FROM TheSavedQuery)
John Spencer said:
True, if you want to edit some values then you will have to write the
query differently. I think the following MIGHT work for you
SELECT X.*
FROM YOURTable as X
WHERE X.AutoNumber IN
(SELECT YourTable.AutoNumber
FROM YourTable INNER JOIN
(SELECT YourTable.BuildingNumber, Max(YourTable.Date) as
LatestDate
FROM YourTable
GROUP BY BuildingNumber) as Temp
ON YourTable.BuildingNumber = Temp.BuildingNumber
AND YourTable.[Date] = Temp.LatestDate)
message Wow, that works great, but now it looks like my records are locked out
from
editing on the form I'm using.
:
Assumption that you want the LATEST Date for each building
Solution
Build and save a query that gets the building number and the latest
date
(use Max)
Use that query and your table in another query, joining the table and
query
on building number and dates
In SQL view that would look like
Query 1:
SELECT BuildingNumber, Max([Date]) as LatestDate
FROM YourTable
GROUP BY BuildingNumber
Query 2:
SELECT YourTable.*
FROM YourTable INNER JOIN Query1
ON YourTable.BuildingNumber = Query1.BuildingNumber
AND YourTable.[Date] = Query1.LatestDate
All in one query: (only possible if your table and field names do
NOT
contain spaces)
SELECT YourTable.*
FROM YourTable INNER JOIN
(SELECT YourTable.BuildingNumber, Max(YourTable.Date) as
LatestDate
FROM YourTable
GROUP BY BuildingNumber) as Temp
ON YourTable.BuildingNumber = Temp.BuildingNumber
AND YourTable.[Date] = Temp.LatestDate
in
message I should've been able too figure this out on my own, but I can't
seem
to
get
any of the criteria I've seen here to work right. Anyways this
should
be
a
simple one for the pros. I've got a monster table and I need to
run a
query
to return only the most recent records. It reads something like
this:
AutoNumber BuildingNumber Date
[fields
3-240]
[AutoNumber] a1 6/6/06 [each
field's value]
[AutoNumber] a1 12/6/06
[each
field's value]
[AutoNumber] a2 6/6/06 [each
field's value]
[AutoNumber] a2 9/18/06
[each
field's value]
etc. for about 1800 more
Any ideas on the criteria I could use? Do I need to make multiple
tables
to
do it?