For the first update step (suppose table A is named Table_1):
Update Table_1 set Quantity = t.total from Table_1 As p inner join (select Min(Col1) as Col1,SUM(quantity) as total from Table_1 group by Col2,Col3) as t on p.Col1=t.Col1
this will update each row with more than one row with a SUM quantity.
then you can delete the same line whose code2 has the same value:
WITH CTE AS( SELECT RN = ROW_NUMBER()OVER(PARTITION BY Col2,Col3 ORDER BY Col2,Col3) FROM Table_1 ) DELETE FROM CTE WHERE RN > 1;
Sorry, I thought Col2 will always be the same with Col3. * I edited my statements. If you get more than 1 line, this can delete everything except the first line.
source share