Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Deletion of duplicates without ID field?

Author  Topic 

Johnph
Posting Yak Master

103 Posts

Posted - 2014-06-17 : 15:06:57
I need to delete duplicates off a table. Duplicates are reliate on specific columns (COL1, COL2, COL3):

TABLE1

COL1,COL2,COL3,COl4,COL5
A,A,A,B,B
A,A,A,B,C
A,A,B,B,B
A,A,A,H,H
B,B,B,F,F
B,B,B,E,E

For table1, I want to get rid of a rows with matching COL1, COl2, and COl3. The results should be like this:

TABLE1

COL1,COL2,COL3,COl4,COL5
A,A,A,B,B
A,A,B,B,B
B,B,B,F,F

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-06-17 : 15:16:20
Search here for "deleting duplicates". You'll be grouping on multiple columns.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

mmkrishna1919
Yak Posting Veteran

95 Posts

Posted - 2014-06-18 : 03:34:16
Try this?

with cte(col1,col2,col3,col4,col5,rn) as
(select t1.*,row_number() over (partition by t1.col1,t1.col2,t1.col3 order by t1.col5 asc) rn
from sqlteam t1
inner join sqlteam t2 on
t1.COL1 = t2.col1
and t1.COL2 = t2.COL2
and t1.COL3 = t2.COL3)

select col1,col2,col3,col4,col5 into #test
from cte where rn=1

select * from #test


M.MURALI kRISHNA
Go to Top of Page
   

- Advertisement -