UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Most Current Address Query    
 
   
jdepinet
post Jan 4 2011, 06:50 PM
Post #1

UtterAccess Veteran
Posts: 328
From: Indianapolis Indiana



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
Go to the top of the page
 
+
Alan_G
post Jan 4 2011, 07:00 PM
Post #2

Utterly Yorkshire and Forum/Wiki Editor
Posts: 15,895
From: Devon UK



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
Go to the top of the page
 
+
Doug Steele
post Jan 4 2011, 07:01 PM
Post #3

UtterAccess VIP
Posts: 17,797
From: Don Mills, ON (Canada)



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

Go to the top of the page
 
+
jdepinet
post Jan 4 2011, 07:04 PM
Post #4

UtterAccess Veteran
Posts: 328
From: Indianapolis Indiana



Still returns all of the addresses
Go to the top of the page
 
+
jdepinet
post Jan 4 2011, 07:20 PM
Post #5

UtterAccess Veteran
Posts: 328
From: Indianapolis Indiana



Doug, still showing all records. I only want to show the newest entries by Owner. Any more suggestions? John
Go to the top of the page
 
+
Doug Steele
post Jan 4 2011, 07:32 PM
Post #6

UtterAccess VIP
Posts: 17,797
From: Don Mills, ON (Canada)



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?
Go to the top of the page
 
+
RickA
post Jan 4 2011, 07:32 PM
Post #7

New Member
Posts: 19



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
Go to the top of the page
 
+
Doug Steele
post Jan 4 2011, 07:36 PM
Post #8

UtterAccess VIP
Posts: 17,797
From: Don Mills, ON (Canada)



Good catch, Rick. I misinterpretted the fields in the table: your approach would appear to be correct.

Sorry about that, jdepinet.
Go to the top of the page
 
+
jdepinet
post Jan 4 2011, 07:53 PM
Post #9

UtterAccess Veteran
Posts: 328
From: Indianapolis Indiana



It works perfect! Thanks Doug and Rick. John
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 19th June 2013 - 07:07 AM