Thursday, 21 February 2013

delete duplicate record from table



WITH tablealias (id, DuplicateCount)
AS
(
SELECT id,ROW_NUMBER() OVER(PARTITION BY id ORDER BY id) AS DuplicateCount FROM emp
)
DELETE FROM tablealias WHERE DuplicateCount > 1
go

//with sub query
select * from (
SELECT id,ROW_NUMBER() OVER(PARTITION BY id ORDER BY id) AS DuplicateCount FROM emp
)tt where DuplicateCount>1

// we can take more than one fields with table

No comments:

Post a Comment

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...