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
Why functions are used in sql server?
What is the maximum row of a size?
How can we determine what objects a user-defined function depends upon?
What are the types of processing and explain each? : sql server analysis services, ssas
How will you monitor replication activity and performance? What privilege do you need to use replication monitor? : sql server replication
What do you mean by the term 'normalization'?
What are different types of roles provided by ssrs?
What is faster join or union?
What is a heap?
What is a virtual table in sql?
What is #temp and @table variable in SQL server?
How to locate and take substrings with charindex() and substring() functions?
Help!!!!!!!!!!!! My database has gone offline, it is highlighted as 'Suspect'. Foolishly, i haven't got a recent back up. Is there a way of quickly restoring the database? Thank you
What is difference between line feed ( ) and carriage return ( )?
Name few endpoints exposed by ssrs 2012?