Solved

Find and Mark Duplicate Records in SQL table

Posted on 2011-03-02
4
818 Views
Last Modified: 2012-05-11
Hello,  I am working in SQL Server 2005.  I am trying to mark duplicate records. Let's say I have these records in a table:

  John Bobbit
  John Wayne
  Mark McSteele
  John Bobbit
  Rosario Jake
  Mark McSteele
  Maria Mandrake
  John Willis
  Mark McSteele
  John Bobbit
  John Wayne

First I would need to find which ones contain duplicates.  Then, I'd need to mark them with a number sequence based on the number of duplicates.  For example, in this list
      John Bobbit,
      Mark McSteele, and
      John Wayne have duplicates.  
John Bobbit has 3, Mark McSteele also has 3, but John Wayne has 2.

I want to update a field in this table so they get marked like this:


1  John Bobbit
1  John Wayne
1  Mark McSteele
2  John Bobbit
1  Rosario Jake
2  Mark McSteele
1  Maria Mandrake
1  John Willis
3  Mark McSteele
3  John Bobbit
2  John Wayne

As you can see there's a 1, 2, and 3 mark for both John Bobbit and Mark McSteele; 1 and 2 for John Wayne; but the rest are marked as 1 because they don't have duplicates.

Any ideas?

Thanks!



0
Comment
Question by:TheUndecider
  • 2
  • 2
4 Comments
 

Author Comment

by:TheUndecider
Comment Utility
I'd also would like to point out all of these records have an unique ID that could be used to update the mark field.  
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
try this.
select row_number() over (partition by your_column order by your_column) as seq_num,
       your_column
  from your_table

Open in new window

0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
Comment Utility
To have unique id, you can try this. You can use this query in your UPDATE statement.
select row_number() over (order by your_column) as unique_id,
       row_number() over (partition by your_column order by your_column) as seq_num,
       your_column
  from your_table

Open in new window

0
 

Author Closing Comment

by:TheUndecider
Comment Utility
Thanks for your answer.  This is exactly what I was looking for.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now