I made a mistake in a bulk insert script, so now i have “duplicate” rows with different colX. I need to delete this duplicate rows, but I cant figure out how. To be more precise, I have this:
col1 | col2 | col3 | colX
----+----------------------
0 | 1 | 2 | a
0 | 1 | 2 | b
0 | 1 | 2 | c
0 | 1 | 2 | a
3 | 4 | 5 | x
3 | 4 | 5 | y
3 | 4 | 5 | x
3 | 4 | 5 | z
and I want to keep the first occurrence of each (row, colX):
col1 | col2 | col3 | colX
----+----------------------
0 | 1 | 2 | a
3 | 4 | 5 | x
Thank you for your replies 🙂
Try the simplest approach with Sql Server’s CTE: http://www.sqlfiddle.com/#!3/2d386/2
Data:
Solution:
Output:
Or perhaps this: http://www.sqlfiddle.com/#!3/af826/1
Data:
Solution:
Output: