Dear RLG:
While it is possible to do what you say, it is very probable that it won't
work out very well. The problem will be that data will not likely be
entered with sufficient consistency. I cannot advise you of what to do with
this, so I'll ignore it for the time.
With that said, it appears you have three sets of data to merge. One set
will have matching values in Last Name, Address, City, State, ZipCode, and
Home Phone, with only differing first names. The next set matches all the
above except for Last Name. The third set matches none of these things.
Now, I'm not sure what you would want to do if there were a 3 way match
within either of these sets. Your example shows what to do only where there
is a 2 way match.
First, here is a way to identify the 2 way matches including Last Name:
SELECT MIN([First Name]) & " & " & MAX([First Name]) & [Last Name] AS
CombinedName,
Address, City, State, ZipCode, [Home Phone]
FROM tblContacts
GROUP BY [Last Name], Address, City, State, ZipCode, [Home Phone]
HAVING COUNT(*) = 2
Some notes about this portion:
- If any of the columns do not match perfectly, the process will fail. If
one Address is "123 Pine" and another is "123 Pine St" then they will not
match. If a space, paren, or hyphen in the Phone is missing or misplaced,
they will not match. This will require extreme precision, nearly
perfection, in the data entry or it will fail.
- If there are 3 persons with the same LastName and the other common
columns, they will not be included. You need to decide what you want to do
with these before programming.
- There is no way to distinguish male and female in this, so putting the
males name first will not be possible. I have put the alphabetically first
name first.
For the second set:
SELECT FIRST([First Name] & " " & [Last Name]) " & "
LAST([First Name] & " " & [Last Name]) AS CombinedName,
Address, City, State, ZipCode, [Home Phone]
FROM tblContacts
WHERE FIRST([Last Name]) <> LAST([Last Name])
GROUP BY Address, City, State, ZipCode, [Home Phone]
HAVING COUNT(*) = 2
Similar notes apply to this portion.
Finally, for the unrepeated persons:
SELECT FIRST([First Name]) & " " & FIRST([Last Name]) AS CombinedName,
Address, City, State, ZipCode, [Home Phone]
FROM tblContacts
GROUP BY Address, City, State, ZipCode, [Home Phone]
HAVING COUNT(*) = 1
After making sure these work correctly, take them and make a single query
out of them, putting UNION ALL between them:
SELECT MIN([First Name]) & " & " & MAX([First Name]) & [Last Name] AS
CombinedName,
Address, City, State, ZipCode, [Home Phone]
FROM tblContacts
GROUP BY [Last Name], Address, City, State, ZipCode, [Home Phone]
HAVING COUNT(*) = 2
UNION ALL
SELECT FIRST([First Name] & " " & [Last Name]) " & "
LAST([First Name] & " " & [Last Name]) AS CombinedName,
Address, City, State, ZipCode, [Home Phone]
FROM tblContacts
WHERE FIRST([Last Name]) <> LAST([Last Name])
GROUP BY Address, City, State, ZipCode, [Home Phone]
HAVING COUNT(*) = 2
UNION ALL
SELECT FIRST([First Name]) & " " & FIRST([Last Name]) AS CombinedName,
Address, City, State, ZipCode, [Home Phone]
FROM tblContacts
GROUP BY Address, City, State, ZipCode, [Home Phone]
HAVING COUNT(*) = 1
Perhaps this is close. I'm not sure it's going to do you much good, but
there it is.
A note about consistency in typing. You have not consistently represented
the names of your columns. For example, some times you have LastName and
some times Last Name. The point is, if it is not so easy for you to be
consistent about something that really has only a single correct technical
spelling, how can you expect users to type in street names with consistency.
It is probably unlikely, unless you start with a database of all correct
street names in each city, and them limit users to selecting the street
names from that list.
Rigorously limiting choices like this is the proven way of getting
consistent data, which can then be used to perform functions like that which
you desire with a high degree of success.
Tom Ellison
rlg said:
I'm using the following sql to create a list of customer to create mailing
labels. However, some of the rows have duplicate addresses. I like to
create one row and concatenate the First and Last name of the duplicate rows
SELECT DISTINCT tblContacts.FirstName, tblContacts.LastName,
tblContacts.Address, tblContacts.City, tblContacts.State,
tblContacts.ZipCode, tblContacts.HomePhone, tblContacts.ContactID,
[FirstName] & " " & [LastNAme] AS expr1
FROM tblContacts INNER JOIN tblPolicy ON tblContacts.ContactID =
tblPolicy.ContactID
WHERE (((tblPolicy.Status)="Active"))
ORDER BY tblContacts.Address, tblContacts.City, tblContacts.State,
tblContacts.ZipCode;
Sample Data:
First Name Last Name Address City State ZipCode Home Phone
Bob Smith 123 Pine Help Mo 63333 (123) 123-1234
Mary Smith 123 Pine Help Mo 63333 (123) 123-1234
Joan Lee 456 Oak June Mo 64545 (343) 323-4343
Betty Jones 456 Oak June Mo 64545 (343) 323-4343
Here's the results I'd like to achive:
CombinedName Address City State ZipCode Home Phone
Bob & Mary Smith 123 Pine Help Mo 63333 (123) 123-1234
Joan Lee & Betty Jones 456 Oak June Mo 64545 (343) 323-4343
Is this possible using SQL or will I need to create VBA code to achive?