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


Please Help Members By Posting Answers For Below Questions

How to enter binary string literals in ms sql server?

584


Which table keeps the locking information?

526


What is transaction server distributed transaction?

561


How to get a list of columns in a view using the "sp_columns" stored procedure?

619


What is the command to change the recovery model?

569






How do you maintain database integrity where deletions from one table will automatically cause deletions in another table?

711


How to get @@error and @@rowcount at the same time?

571


What is the command used to check locks in microsoft sql server?

545


How do I create a trace in sql server?

518


What are .mdf files?

521


How you can minimize the deadlock situation?

555


What is temporary table in sql server? Why we use temp table?

517


What are the steps to process a single select statement?

514


you added a row to a view, but the row is not shown on the view. Explain how this can happen, and how you can remedy the situation

541


User wants only to display only pdf as export option in report manager. How to achieve this?

173