Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


removing duplicates


removing duplicates

Author
Message
Steve Jones
Steve Jones
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: Administrators
Points: 35981 Visits: 18727
Show how to find duplicates based on another field. This can be the primary key, but perhaps not. Perhaps it's a timestamp of some sort.

Show how to query to check and then construct a delete statement based on the select.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Sioban Krzywicki
Sioban Krzywicki
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1134 Visits: 8067
You mean use another existing field to differentiate between duplicates in the rest of the data?

--------------------------------------
When you encounter a problem, if the solution isn't readily evident go back to the start and check your assumptions.
--------------------------------------
It’s unpleasantly like being drunk.
What’s so unpleasant about being drunk?
You ask a glass of water. -- Douglas Adams
Steve Jones
Steve Jones
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: Administrators
Points: 35981 Visits: 18727
Yes, as you could choose a primary key, or if one doesn't exist (or isnt' appropriate) another one. This one is really to teach how someone that doesn't know how to remove a duplicate can do so.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
TomThomson
TomThomson
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10672 Visits: 11991
Does the removing the duplicates job include adding a constraint to make sure the problem doesn't recur?

Tom

Steve Jones
Steve Jones
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: Administrators
Points: 35981 Visits: 18727
Worth mentioning, but not necessary. That's not always a possibility.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44930 Visits: 39857
L' Eomot Inversé (9/7/2013)
Does the removing the duplicates job include adding a constraint to make sure the problem doesn't recur?


I agree that it probably should at least have an honorable mention... except maybe on staging tables that take in data from 3rd party sources.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Michael Meierruth
Michael Meierruth
SSChasing Mays
SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)

Group: General Forum Members
Points: 635 Visits: 2506
Steve,
It is mentioned at
http://www.sqlservercentral.com/Forums/Topic793765-145-1.aspx
It uses CTE with delete. Very neat.

create table t(a varchar(1))
insert into t values('b')
insert into t values('a')
insert into t values('b')
insert into t values('a')

with
dup as
(
select a,row_number() over(partition by a order by a) rn
from t
)
delete from dup where rn>1


In Oracle this would be pretty straight forward via the use of the pseudo rowid column that all tables have.
This references a physical (and thus unique) address of each row.

At this link
http://www.codeproject.com/Articles/159785/Physical-location-of-a-row-in-SQL-Server
there is an interesting discussion of undocumented %%physloc%% and %%lockres%% columns starting with SS2008R2.
Steve Jones
Steve Jones
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: Administrators
Points: 35981 Visits: 18727
Bump

Looking for a writeup here as an article.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Eirikur Eiriksson
Eirikur Eiriksson
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6691 Visits: 17680
Would this be of an interest?

Data de-duplication using 2012 window functions.

Simple cases;
1. A set with no surrogate key defined
2. A set with a surrogate key, related records must be updated
Complex cases;
1. Retaining the first value by any chosen sequence
2. Retaining the first good value by any chosen sequence and pattern
3. Retaining the last value by any chosen sequence
4. Retaining the last good value by any chosen sequence and pattern
5. Retaining the most dense value by any chosen pattern
6. Retaining the least dense value by any chosen pattern
Business rule application (possibly a separate article);
1. Rule controlled matching
2. Rule controlled de-duplication
Steve Jones
Steve Jones
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: Administrators
Points: 35981 Visits: 18727
These would be good. We'd be looking for 2-3 per article to keep this relatively short.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
a4apple
a4apple
SSC-Enthusiastic
SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)

Group: General Forum Members
Points: 170 Visits: 406
Steve, here is a step by step article I wrote on how to remove duplicates from a table in SQL Server. Still waiting for approval for the previous submitted articles, so posting the original link for my website for now. see if this helps.

http://sqlsaga.com/sql-server/how-to-remove-duplicates-from-a-table-in-sql-server/


Good Luck Smile .. Visit www.sqlsaga.com for more t-sql code snippets and BI related how to articles.
Steve Jones
Steve Jones
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: Administrators
Points: 35981 Visits: 18727
Eirikur Eiriksson (2/16/2014)
Would this be of an interest?

Data de-duplication using 2012 window functions.

Simple cases;
1. A set with no surrogate key defined
2. A set with a surrogate key, related records must be updated
Complex cases;
1. Retaining the first value by any chosen sequence
2. Retaining the first good value by any chosen sequence and pattern
3. Retaining the last value by any chosen sequence
4. Retaining the last good value by any chosen sequence and pattern
5. Retaining the most dense value by any chosen pattern
6. Retaining the least dense value by any chosen pattern
Business rule application (possibly a separate article);
1. Rule controlled matching
2. Rule controlled de-duplication


I think the simple cases are covered.

It would be good to see an article that deletes the first or last value by some sequence. A separate piece could deal with having a pattern.

I hadn't thought about density, but there's an article in there as well.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search