SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Deletion of duplicates without ID field?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Johnph
Yak Posting Veteran

95 Posts

Posted - 06/17/2014 :  15:06:57  Show Profile  Reply with Quote
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

Edited by - Johnph on 06/17/2014 15:10:06

tkizer
Almighty SQL Goddess

USA
36851 Posts

Posted - 06/17/2014 :  15:16:20  Show Profile  Visit tkizer's Homepage  Reply with Quote
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

India
85 Posts

Posted - 06/18/2014 :  03:34:16  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000