0

The main table is Table2 and I want to delete duplication RollID in Table1 but retaining 1 like the example below 6456 (Note: Primary Keys Table ID's are auto-incremental)

Table 1 (child table)

Table1_ID   RollID
 1          6456 
 2          6456 
 3          6459 

Table2 (Parent)

Table2_ID    RollID
 1            6456 
 2            6459 

How can I attain this scenario.. Your helping hand is highly appreciated...

JNevill
  • 42,519
  • 3
  • 32
  • 55
Jake
  • 73
  • 7

2 Answers2

0

Try this:

WITH CTE AS(
   SELECT Table1_ID, RollID,
       RN = ROW_NUMBER()OVER(PARTITION BY RollID ORDER BY RollID)
   FROM dbo.Table1
)
DELETE FROM CTE WHERE RN > 1
Rahul Tripathi
  • 161,154
  • 30
  • 262
  • 319
0
DELETE FROM Table1 WHERE ID NOT IN (SELECT MIN(ID) FROM Table1 GROUP BY ROLLID)
Dmitrij Kultasev
  • 5,173
  • 3
  • 43
  • 79