Solved

Find and Mark Duplicate Records in SQL table

Posted on 2011-03-02
4
828 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
ID: 35022789
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
ID: 35022820
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
ID: 35022848
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
ID: 35028692
Thanks for your answer.  This is exactly what I was looking for.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

832 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