5

This the name of my table Result_Simul

This is the value

 Pk FkIdResult FkIdSimul 1 43 1244 2 43 1244 3 52 1244 4 52 1244 

How to keep just keep rows Pk=1 and Pk=3 and delete Pk=2 and Pk=4

Thanks for helping me.

Im not really good in Tsql

Frank

1
  • What version of sql-server are you on? Commented Mar 23, 2012 at 19:03

1 Answer 1

15

You can use row_number to give each duplicate an ascending number, and then delete the 2nd and higher duplicates:

delete tbl from ( select row_number() over (partition by FkIdResult, FkIdSimul order by Pk desc) as rn , * from YourTable ) tbl where rn > 1 

Working example at SE Data.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.