top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

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

0 votes
78 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
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...