dbTalk Databases Forums  

Identifying Duplicates in a table

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Identifying Duplicates in a table in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
chudson007@hotmail.com
 
Posts: n/a

Default Identifying Duplicates in a table - 03-11-2006 , 05:31 AM






I need help flagging duplicate records in ome tables I have.

For example if I have Table1 which conatins Field1, Field2 and Field3
like below

Field1 Field2 Field3 Field4
Paul 18 Null Null
Paul 18 Null Null
John 19 Null Null

How would I;
1. put a 'Y' in Field3 to mark the two records which are duplicates.
2. put a 'Y' in Field4 to mark ONLY ONE of the duplicate records.

Regards,

Ciarán


Reply With Quote
  #2  
Old   
Miks
 
Posts: n/a

Default Re: Identifying Duplicates in a table - 03-11-2006 , 09:32 AM






Pls check this article

http://www.sqlservercentral.com/colu...aterecords.asp

Rgds,
Miks


Reply With Quote
  #3  
Old   
Miks
 
Posts: n/a

Default Re: Identifying Duplicates in a table - 03-12-2006 , 10:46 AM



I have tried, My soultion may not be 100% perfect, Corrections welcome.

CREATE TABLE [dbo].[test2] (
[Name] [char] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[Age] [int] NOT NULL ,
[flag] [int] NULL ,
[flag1] [int] NULL
) ON [PRIMARY]
GO

Table Values

Alen 19 0 0
Alen 19 0 0
Aex 20 0 0

Code
-----

declare @name varchar(20), @age int, @counts int

declare cust cursor for
select count(1), Name, Age from test2 group by Name, Age having
count(Name) > 1

open cust fetch next from cust
into @counts, @name, @age

while @@fetch_status = 0

if(@counts) = 2
update test2 set flag = 1 where Name = @name and Age = @age

fetch next from cust into @counts, @name, @age

close cust
deallocate cust


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.