|
-
Sep 30th, 2008, 10:03 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] [Access SQL] Pull only 1 record per account from Customer.
How would I create a query, that pulls only 1 customer name from the customer table per account?
Tables
AccountTable
AccountNumber (PK)
Address
OtherInfo, etc...
CustomerTable
AccountNumber
FullName
If I do a left, inner, or right join, it will still get the incorrect number of records.
For instance.
Account table has 2 accounts.
1, 123 main street
2, 443 apple ave
Customer has 3 names
1, John
1, Jane
2, Bob
The returned results would be
1, 123 main street, John
1, 123 main street, Jane
2, 443 apple ave, Bob
How do I make it pick only 1 customer name, so that I am not duplicating address information?
Please RATE posts, click the RATE button to the left under the Users Name.
Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.
"As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood
-
Oct 1st, 2008, 10:10 PM
#2
Addicted Member
Re: [Access SQL] Pull only 1 record per account from Customer.
-
Oct 2nd, 2008, 06:21 AM
#3
Re: [Access SQL] Pull only 1 record per account from Customer.
Usually you would do that with a group by or a sub query as Tiom suggests. A group by is better but slightly less flexible. The first thing you need to decide though, is how you're going to decide which name to pull.
For example:-
Code:
Select AccountNumber, max(FullName)
From AccountTable
Inner Join CustomerTable
on AccountTable.AccountNumber = CustomerTable.AccountNumber
Group By AccountNumber
Would return you 1, John because John is 'higher' than Jane (lucky John). Changing the Max to a Min would have returned Jane instead.
If a max or a min is enough to decide which name to get then a group by is your best bet. If it's more complicated you're probably better off with a sub query:-
Code:
Select AccountNumber, (Select FullName
From CustomerTable
Where CustomerTable.AccountID = AccountTable.AccountID
And enough other criteria to identify a unique name)
From AccountTable
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Oct 2nd, 2008, 03:31 PM
#4
Thread Starter
Fanatic Member
Re: [Access SQL] Pull only 1 record per account from Customer.
Nice, so If I wanted to get just the newest fullname, I could add an import date, or update data to the customer table.
Then do the sub query like both of you suggested with the restriction of TOP 1, and do an ORDER BY ImportDate.
Code:
Select AccountNumber, (Select TOP 1 FullName
From CustomerTable
Where CustomerTable.AccountID = AccountTable.AccountID
ORDER BY CustomerTable.ImportDate)
From AccountTable
Or would I need to code it different to get the newest entered one?
Please RATE posts, click the RATE button to the left under the Users Name.
Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.
"As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood
-
Oct 3rd, 2008, 03:39 AM
#5
Re: [Access SQL] Pull only 1 record per account from Customer.
That's spot on. Basically, you can define the order in any way you like as long as there are fields to support it. So you could pick the wealthiest customer, the tallest, the last added or the most likely to go on a killing spree. As long as you've got the fields to support it you just drop in the apropriate order by and select the top 1.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Oct 3rd, 2008, 01:27 PM
#6
Thread Starter
Fanatic Member
Re: [Access SQL] Pull only 1 record per account from Customer.
Right on! It works
Thank you guys =)
Please RATE posts, click the RATE button to the left under the Users Name.
Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.
"As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|