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
  • 46,980
  • 4
  • 38
  • 63
Jake
  • 93
  • 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
  • 168,305
  • 31
  • 280
  • 331
0
DELETE FROM Table1 WHERE ID NOT IN (SELECT MIN(ID) FROM Table1 GROUP BY ROLLID)
Dmitrij Kultasev
  • 5,447
  • 5
  • 44
  • 88