473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Merging Duplicate Rows

Hello All,

I have an issue with dupliate Contact data. Here it is:

I have a Contacts table;

CREATE TABLE CONTACTS
(
SSN int,
fname varchar(40),
lname varchar(40),
address varchar(40),
city varchar(40),
state varchar(2),
zip int
)

Here is some sample data:

SSN: 1112223333
FNAME: FRANK
LNAME: WHALEY
ADDRESS: NULL
CITY: NULL
STATE NY
ZIP 10033

SSN: 1112223333
FNAME: NULL
LNAME: WHALEY
ADDRESS: 100 MADISON AVE
CITY: NEW YORK
STATE NY
ZIP NULL

How do I merge the 2 rows to create one row as follows:
via SQL or T-SQL

SSN: 1112223333
FNAME: FRANK
LNAME: WHALEY
ADDRESS: 100 MADISON AVE
CITY: NEW YORK
STATE NY
ZIP 10033

Pointers appreciated.
Thanks

Jul 23 '05 #1
5 9195
On 16 Feb 2005 13:18:24 -0800, hharry wrote:
I have an issue with dupliate Contact data. Here it is: (snip)How do I merge the 2 rows to create one row as follows:
via SQL or T-SQL

(snip)

Hi hharry,

The following is untested, since you didn't include the INSERT statements
to recreate your data on my system, but I believe it should work:

SELECT SSN, MAX(Fname) AS Fname), MAX(LName) AS LName,
MAX(Address) AS Address, MAX(City) AS City,
MAX(State) AS State, MAX(ZIP) AS ZIP
FROM Contacts
GROUP BY SSN

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2
One very naive method would be the following:

INSERT INTO NewTable (ssn, fname, lname, address, city, state, zip)
SELECT ssn, MAX(fname), MAX(lname),
MAX(address), MAX(city), MAX(state), MAX(zip)
FROM Contacts
GROUP BY ssn

but it really depends on what rules you want for the data cleansing.
For example, what result would you want if Frank moved from New York to
Seattle but the WA state code wasn't entered? The above query would
return an invalid address: Seattle, NY. This is where specialist
address validation software is useful.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #3

"hharry" <pa*********@nyc.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
Hello All,

I have an issue with dupliate Contact data. Here it is:

I have a Contacts table;

CREATE TABLE CONTACTS
(
SSN int,
fname varchar(40),
lname varchar(40),
address varchar(40),
city varchar(40),
state varchar(2),
zip int
)

Here is some sample data:

SSN: 1112223333
FNAME: FRANK
LNAME: WHALEY
ADDRESS: NULL
CITY: NULL
STATE NY
ZIP 10033

SSN: 1112223333
FNAME: NULL
LNAME: WHALEY
ADDRESS: 100 MADISON AVE
CITY: NEW YORK
STATE NY
ZIP NULL

How do I merge the 2 rows to create one row as follows:
via SQL or T-SQL

SSN: 1112223333
FNAME: FRANK
LNAME: WHALEY
ADDRESS: 100 MADISON AVE
CITY: NEW YORK
STATE NY
ZIP 10033

Pointers appreciated.
Thanks


Based on your sample data, your table has no primary key, or it would not be
possible to have multiple rows with a single SSN. I strongly suggest you add
a primary key to correct your data model and prevent these duplicates,
otherwise you will continue to get bad data in your table. As a quick fix,
you may be able to do something like this:

update dbo.Contacts
set fname = coalesce(c1.fname, c2.fname),
lname = coalesce(c1.lname, c2.lname),
address = coalesce(c1.address, c2.address)
/* etc */
from dbo.Contacts c1
join dbo.Contacts c2
on c1.SSN = c2.SSN

But this assumes that you have only two rows (maximum) per SSN, and there
are no cases where there are different fname or lname values for each SSN,
only one with a NULL and one without. What do you do if one row has fname
'Steven' and another one has fname 'Stephen' with the same SSN? There is no
way to decide automatically which is correct - you have to find out from
some other source.

Simon
Jul 23 '05 #4
Thanks All,

I will add some clarification. I have a base Contacts table and receive
updates in flat file format as the base table data is incomplete. Now,
I planned on loading the flat file data into a temp table with the same
structure as the base table and then updating incomplete base table
rows with values from the temp table, matching on SSN.

For example, Frank Whaley has missing addess information which is
located in the temp table...how do i update the base with the temp
data, assuming I don't know in advance which column data is incomplete
?

Thanks

Jul 23 '05 #5
So you now have two tables instead of one. Can I assume that SSN is the
key in both? Please include DDL with future posts so that we don't have
to guess these things. Try this:

UPDATE C
SET fname = COALESCE(C.fname, C2.fname),
lname = COALESCE(C.lname, C2.lname),
address = COALESCE(C.address, C2.address),
city = COALESCE(C.city, C2.city),
state = COALESCE(C.state, C2.state),
zip = COALESCE(C.zip, C2.zip)
FROM Contacts AS C
JOIN Contacts2 AS C2
ON C.ssn = C2.ssn

--
David Portas
SQL Server MVP
--

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Klatuu | last post by:
Whew, I've struggled my way through figuring out how to use XML to transport data..now I can imagine what having a baby is like :) But, I'm stuck now. I generate the XML (single table, no...
0
by: Walt Borders | last post by:
Hi, My problem: Merging two datasets deletes parent elements, preserves all children. I've created two dataSets. Each use the same schema, parent-child nested tables. The first dataSet is...
2
by: Emmett Power | last post by:
Hi, I have an Access table with a number of records which refer to the same person but with data in different fields. So for example the table would look like this: Name..............Field...
12
by: google_groups3 | last post by:
Hi all. I currently have 2 text files which contain lists of file names. These text files are updated by my code. What I want to do is be able to merge these text files discarding the...
0
by: B.N.Prabhu | last post by:
Hi, I have a DataTable with several rows. Its having 20 Columns. when i click the Insert button then i have to check the Database Rows. Whether these new rows are already available in the...
3
by: Ralph Smith | last post by:
I have two identical databases on two different servers and I need to add the data in tables from one server to the tables in the other server. Is there a way to do that in mysql? thanks, Ralph
6
by: dannylam4 | last post by:
Hello, I've got a question about merging/concatenating rows. There's a similar topic here: Combining Multiple Rows of one Field into One Result but I didn't know if I should hijack it. Basically, I...
2
by: Neil Chambers | last post by:
I am trying to get my head around dataset merging but despite a little research I could still use a pointer (or ten). Basically I want to perform an outer join operation on a dataset (created from...
1
by: akdemirc | last post by:
Hi, My question is about retrieving single records based on a time column, i mean the result set should not include duplicate rows for a unique time value as an example: A B C ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.