Full Version: Most Current Address Query
UtterAccess Discussion Forums > Microsoft® Access > Access Queries
jdepinet
This seems easy but I'm lost.....I have tblAddress the contains:

pkAddress
fkOwner
ingNumber
strStreet
strCity
ingZip
dtDateEntered

The user can add as many addresses as needed per owner and everytime a new address is added it is time stamped. I want a query show just the most current address for each owner. Any Help? Thanks John
Alan_G
Hi

Create a new query using the Query Designer. Add the tables and the fields from them into the query design grid, then turn the query into a Totals query, when you'll see a new row added to the design grid. Select Max from the drop down in the dtDateEntered field and try running the query
Doug Steele
SELECT pkAddress, fkOwner, ingNumber, strStreet, strCity, ingZip, dtDateEntered
FROM tblAddress INNER JOIN
(SELECT pkAddress, Max(dtDateEntered) As LatestEntry
FROM tblAddress
GROUP BY pkAddress) AS Subq
ON tblAddress.pkAddress = Subq.pkAddress
AND tblAddress.dtDateEntered = Subq.LatestEntry
ORDER BY pkAddress

jdepinet
Still returns all of the addresses
jdepinet
Doug, still showing all records. I only want to show the newest entries by Owner. Any more suggestions? John
Doug Steele
Something's wrong with the data then.

Try this. Run the following query:

SELECT pkAddress, Max(dtDateEntered) As LatestEntry
FROM tblAddress
GROUP BY pkAddress

Do you get only one row for each value of pkAddress?

Assuming you do only get one row for each value of pkAddress, look closely at the results of the original query. Do each of the duplicate rows have the same value for dtDateEntered?
RickA
Try this:

SELECT pkAddress, fkOwner, ingNumber, strStreet, strCity, ingZip, Subq.LatestEntry
FROM tblAddress
INNER JOIN
(SELECT fkOwner, Max(dtDateEntered) As LatestEntry
FROM tblAddress
GROUP BY fkOwner) As Subq
ON tblAddress.fkOwner= Subq.fkOwner
AND tblAddress.dtDateEntered = Subq.LatestEntry
Doug Steele
Good catch, Rick. I misinterpretted the fields in the table: your approach would appear to be correct.

Sorry about that, jdepinet.
jdepinet
It works perfect! Thanks Doug and Rick. John
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.