/*
Create table TempTable
(
ID INT,
FName nvarchar(50),
LName nvarchar(50)
)
GO
INSERT TempTable Values ('1','XYZ','PQR')
INSERT TempTable Values ('1','XYZ','PQR')
INSERT TempTable Values ('1','XYZ','PQR')
INSERT TempTable Values ('2','ABC','DEF')
INSERT TempTable Values ('2','ABC','DEF')
INSERT TempTable Values ('2','ABC','DEF')
GO
SELECT * FROM TempTable
SELECT ID,
COUNT(ID) AS NumOccurrences
FROM TempTable
GROUP BY ID
HAVING ( COUNT(ID) > 1 )
Delete from TempTable
/*
-------------//////////////Another Way///////////---------------
SELECT * FROM TempTable
SELECT ID,
COUNT(ID) AS NumOccurrences
INTO holdkey
FROM TempTable
GROUP BY ID
HAVING ( COUNT(ID) > 1 )
SELECT DISTINCT TempTable.*
INTO holddups
FROM TempTable, holdkey
WHERE TempTable.ID = holdkey.ID
SELECT ID, count(*)
FROM holddups
GROUP BY ID
DELETE TempTable
FROM TempTable, holdkey
WHERE TempTable.ID = holdkey.ID
INSERT TempTable SELECT * FROM holddups
*/
/*
-------------//////////////Another Way///////////---------------
Select * From TempTable
set rowcount 1
select 'start'
while @@rowcount > 0
delete a from TempTable a where (select count(*) from TempTable b
where a.ID = b.ID)>1
set rowcount 0
select * from TempTable
set nocount off
*/
Thanks a lot
ReplyDelete