[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 46
  • Last Modified:

Select unique records (members per household) in access

I need to select unique records from a table in access.  Setup first.

My table design is as follows - all text fields.

Transaction Name
Household Name
First Name
Last Name
Mailing Street
Mailing City
Mailing State/Province
Mailing Zip/Postal Code

I need to get only one full mailing address (First Name, Last Name, Mailing Street, Mailing City, Mailing State/Province, Mailing Zip/Postal Code) per distinct Household Name.

Can I do this with a subquery?
0
mattturley
Asked:
mattturley
  • 4
  • 4
  • 2
1 Solution
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
What is the primary key for that table?
0
 
mattturleyAuthor Commented:
None.
0
 
mattturleyAuthor Commented:
I could make it transaction name.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Should be a column that can identify an unique record.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Assuming Transaction Name as PK this can be a solution:
SELECT T1.FirstName, T1.LastName, T1.MailingStreet, T1.MailingCity, T1.MailingState, T1.MailingZip
FROM YourTableNameHere T1
WHERE T1.TransactionName = (SELECT TOP 1 T2.TransactionName 
                                                    FROM YourTableNameHere T2
                                                     WHERE T2.MailingStreet=T1.MailingStreet
                                                          AND T2.MailingCity=T1.MailingCity
                                                          AND T2.MailingState=T1.MailingState
                                                          AND T2.MailingZip=T1.MailingZip)

Open in new window

0
 
mattturleyAuthor Commented:
I'm a little confused - are you creating a table or aliasing part of the query as a table?
0
 
Helen FeddemaCommented:
It would be better to have a separate Households table, with a HouseholdID key field.  Then put that key into the Persons table, as a foreign key.  The address fields should be in tblHouseholds, which would have one record per household.  Though you might run into problems with divorced couples, where a child could be moving between two households in a joint custody arrangement.
0
 
Helen FeddemaCommented:
Having the address repeated for each person means that if there is a change of address, you will have to go into each person's record and change the address; if they are in a Households table, you only need to change it once.

What is a transaction?  Depending on the answer to that question, you might need yet another table.
0
 
mattturleyAuthor Commented:
While I appreciate the best practice on table structure, naming, field names, etc., I am just working with what was given to me.  Would prefer a solution that keeps the query going against the single table, as identified in the question.
Thanks!
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
No new tables. Only aliases since the query works with the same table twice, so you need to provide alias for the engine know to each one are you referring to.
Did you test it?
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now