How to retrieve duplicate rows in a table?
How to delete the duplicate entries in a table?
Answer Posted / sameer
--select * from #TempR
select * into #temp2 from #TempR
-- select * from #temp2
alter table #temp2 add record_id numeric(5,0) identity not
null
/* select those row which are repeated */
select * into #qwe
from #temp2
where exists(
select null from #temp2 b
where b.ID = #temp2.ID
and b.TYPE = #temp2.TYPE
group by b.ID, b.TYPE
having
count (*) >=2
)
--select * from #qwe
/* delete those row which are repeted */
delete from #TempR where ID in ( select ID from #qwe)
/* insert those row which are deleted */
delete from #qwe where record_id not in (
select record_id
from #qwe
group by ID, TYPE
having record_id = max (record_id)
)
-- select * from #qwe
alter table #qwe drop record_id
insert into #TempR
select * from #qwe
/* see output */
select * from #TempR
/* check for row getting repeted */
select *
from #TempR
where exists(
select null from #TempR b
where b.ID = #TempR.ID
and b.TYPE = #TempR.TYPE
group by b.TT_ID, b.EQP_TYPE
having
count (*) >=2
)
| Is This Answer Correct ? | 1 Yes | 1 No |
Post New Answer View All Answers
What is indexing in sql server with example?
What is full outer join in sql server joins?
What is global temp table?
Explain security with sql azure?
What stored by the master?
How to throw custom exception in Stored Procedure?
Explain isolation levels that sql server supports?
List out the differences between the clustered index and non-clustered index in sql server?
What is log shipping?
What is data modeling and Reterminal integrity?
what is the difference between a primary key and a unique key? : Sql server database administration
Do you know what is bit data type and whats the information that can be stored inside a bit column?
You notice that the transaction log on one of your databases is over 4gb the size of the data file is 2mb what could cause this situation, and how can you fix it?
how to restart sql server in single user mode? How to start sql server in minimal configuration mode? : Sql server database administration
Which table keeps information about stored procedures?