Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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 drop existing views from a database in ms sql server?

1096


Tell me what do we need queues in sql service broker?

982


How to view existing indexes on an given table using sp_help?

1124


You schedule a job to run every minute what will happen if the first job runs more than 1 min? Will the second instance of the job start?

1028


Is ssrs support other database except ms sql server?

139


What is the difference between drop table and truncate table?

918


What is use of except clause? How does it differ from not in clause?

1015


How to drop existing indexes in ms sql server?

1069


What is open database communication (odbc)?

1056


Explain where clause?

984


Explain what is cte (common table expression)?

1119


What is merge replication?

1098


What is spid in sql server profiler?

1093


How to handle error or exception in sql?

975


What do you understand by coalesce in sql server?

1063