top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How we can delete duplicate rows in a table with no unique key in SQL server ?

0 votes
513 views
How we can delete duplicate rows in a table with no unique key in SQL server ?
posted Apr 6, 2017 by Snehal Raikar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
create table #t1(col1 int, col2 int, col3 char(50))
insert into #t1 values (1, 1, 'data value one')
insert into #t1 values (1, 1, 'data value one')
insert into #t1 values (1, 2, 'data value two')

;WITH [CTE_DUPLICATES] AS 
(
SELECT RN = ROW_NUMBER() OVER (PARTITION BY col2 ORDER BY col1)
FROM #t1
) 
DELETE FROM [CTE_DUPLICATES] WHERE RN > 1

select * from #t1

drop table #t1
answer Apr 8, 2017 by Shivaranjini
...