beyondrelational.com



Delete Duplicate Records

rated by 0 users
Answered (Verified) This post has 1 verified answer | 7 Replies | 5 Followers

Top 100 Contributor
6 Posts
Points 80
guptaS posted on 01-27-2010 10:31 AM

Hi Guys,,,

I want to delete duplicate records from my table and there is no primary key which i can use in this and all the values in rows are identical.

identity              name

1                      Sumit

1                      Sumit

2                    Ajay

2                  Ajay

 

So i just want to get only 2 rows in this

identity       name

1               Sumit

2              Ajay

This is  dummy data only i have many other columns in my table

and i am also not able to use ranking functions as still we are on SQL 2000

Hope you guys help.

 

Answered (Verified) Verified Answer

Top 10 Contributor
131 Posts
Points 1,305

Divya Agrawal

I formatted your code for better readability

But your method is the most ineffecient among any method that remove duplicate

For the given examples, it would be

 

select distinct * from table 

  • | Post Points: 40

All Replies

Top 500 Contributor
2 Posts
Points 25
Suggested by Bhavani



declare @tab table (id int,name varchar(10))
insert into @tab (id,name)
select 1,'Sumit' union all
select 1,'Sumit' union all
select 2,'Sumit2' union all
select 2,'Sumit2'

select id,name from ( select row_number() over (partition by id,name order by id )as row ,id,name from @tab )as t
where row= 1

  • | Post Points: 20
Top 100 Contributor
6 Posts
Points 80

Thanks Bhavani,, But here is the problem that i am using SQL Server 2000 in which row_number() function is not available.

Do you have any way to do it in SQL Server 2000

  • | Post Points: 20
Top 25 Contributor
Female
30 Posts
Points 335
 
  
Declare @tbl Table(name varchar(100), number int,id int)
insert into  @tbl 
select 'divya',111,1 union all 
select 'divya',211,1 union all 
select 'divya',311,1 union all 
select 'srinu',111,2 union all 
select 'srinu',211,2 union all 
select 'srinu',311,2 

set  rowcount  1

while exists ( select id from @tbl group by id having count(*) > 1)

begin

	delete from @tbl where id in(select top 1 id from @tbl group by id having count(*) > 1) 
end

set rowcount  0

select * from  @tbl


 There can be other method by adding an identity column

Declare @tbl Table(name varchar(100), number int,id int)
insert into  @tbl 
select 'divya',111,1 union all 
select 'divya',211,1 union all 
select 'divya',311,1 union all 
select 'srinu',111,2 union all 
select 'srinu',211,2 union all 
select 'srinu',311,2 

set  rowcount 1
 

while exists ( select id from @tbl group by id having count(*) > 1 )

begin
	delete from @tbl where id in(select top 1 id from @tbl group by id having count(*) > 1) 
end

set rowcount  0

select * from  @tbl

Another method can be by adding any identity column.

--Never expect yourself to be given a good value... create a value of your own

  • | Post Points: 20
Top 10 Contributor
131 Posts
Points 1,305

Divya Agrawal

I formatted your code for better readability

But your method is the most ineffecient among any method that remove duplicate

For the given examples, it would be

 

select distinct * from table 

  • | Post Points: 40
Top 10 Contributor
Male
92 Posts
Points 785
Yes take data in any table variable using select distinct * and delete all data from orignal table and put distinct data back in the table will work fine for this scenario.
  • | Post Points: 20
Top 150 Contributor
5 Posts
Points 115

 

 

 

 

;

 

with

Dups

as

(

 

 

SELECT *,row_number() OVER (partition by id order by id) as  RowNum FROM  employee

)

Delete 

 

from Dups where rownum > 1 ;

SELECT 

 

* FROM  employee

  • | Post Points: 5
Page 1 of 1 (8 items) | RSS
Copyright © Beyondrelational.com