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
 Keep only one record from duplicate entry
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

kalyan.cse05
Yak Posting Veteran

India
74 Posts

Posted - 02/18/2014 :  04:11:23  Show Profile  Reply with Quote
How can i keep only one record from duplicate entry.
Example
COLUMN1 COLUMN2 COLUMN3 COLUMN4
AAA 121 -1 2
AAA 121 -1 3
AAA 121 -1 4


Here i want to keep only the top record and want to delete other 2 rows. Can someone plz help

kalyan Ashis Dey

webfred
Flowing Fount of Yak Knowledge

Germany
8768 Posts

Posted - 02/18/2014 :  05:22:36  Show Profile  Visit webfred's Homepage  Reply with Quote
So you want to keep the row with the lowest value in column4 and col1 to col3 are same values?

delete from
(select row_number() over(partition by columnn1, column2, column3 order by column4 asc)rn,* from YourTable)dt
where rn > 1



Too old to Rock'n'Roll too young to die.
Go to Top of Page

kalyan.cse05
Yak Posting Veteran

India
74 Posts

Posted - 02/18/2014 :  05:50:57  Show Profile  Reply with Quote
Thanks a lot for your help

kalyan Ashis Dey
Go to Top of Page

mohan123
Constraint Violating Yak Guru

India
252 Posts

Posted - 02/20/2014 :  04:25:32  Show Profile  Reply with Quote
CREATE TABLE #TAble1 (NAME VARCHAR(10),Id INT,ID1 INT,ID2 INT)
INSERT INTO #TAble1(NAME ,Id ,ID1 ,ID2 ) VALUES ('AAA', 121, 1 ,2)
INSERT INTO #TAble1 (NAME ,Id ,ID1 ,ID2 ) VALUES ('AAA', 121, 1, 3)
INSERT INTO #TAble1 (NAME ,Id ,ID1 ,ID2 ) VALUES ('AAA', 121,1, 4)

;WITH CTE AS (
Select NAME,Id,ID1,ID2,ROW_NUMBER()OVER (PARTITION BY ID ORDER BY ID )AS RN FROM #TAble1 t
)
DELETE from CTE WHERE RN = 1
select * from #TAble1

P.V.P.MOhan
Go to Top of Page

maunishq
Yak Posting Veteran

Canada
71 Posts

Posted - 02/21/2014 :  14:22:02  Show Profile  Reply with Quote
@mohan123: It should be RN>1.


!!_(M)_!!
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.12 seconds. Powered By: Snitz Forums 2000