create table testing (retained int, dropped int)insert into testing (retained, dropped)select 767884, 157441 union allselect 1046261, 157441 union allselect 1055257, 157441 union allselect 157441, 73635 union allselect 767884, 73635 union allselect 1046261, 73635 union allselect 1055257, 73635 union allselect 1046261, 767884 union allselect 1055257, 767884 union allselect 1055257, 1046261select * from testing-- consoidate records:-- updates 6 records:update bset b.retained=a.retainedfrom testing as ainner join testing as bon b.retained = a.dropped-- updates remaining 3update bset b.retained=a.retainedfrom testing as ainner join testing as bon b.retained = a.droppedselect * from testingdrop table testing
create table testing (retained int, dropped int)insert into testing (retained, dropped)select 767884, 157441 union allselect 1046261, 157441 union allselect 1055257, 157441 union allselect 157441, 73635 union allselect 767884, 73635 union allselect 1046261, 73635 union allselect 1055257, 73635 union allselect 1046261, 767884 union allselect 1055257, 767884 union allselect 1055257, 1046261select * from testingORDER BY dropped,retained;WITH UpdateValues AS (SELECT MAX(retained) AS NewValue, droppedFROM testingGROUP BY dropped)UPDATE t SET retained = uv.NewValueFROM testing t INNER JOIN UpdateValues uv ON (t.dropped = uv.dropped);select * from testingORDER BY dropped,retained;DROP TABLE testing;
drop table dbo.testing create table dbo.testing (retained int, dropped int)insert into dbo.testing (retained, dropped)select 767884, 157441 union allselect 1046261, 157441 union allselect 6699, 157441 union allselect 157441, 73635 union allselect 767884, 73635 union allselect 1046261, 73635 union allselect 6699, 73635 union allselect 1046261, 767884 union allselect 6699, 767884 union allselect 6699, 1046261
SELECT a.* FROM testing aLEFT JOIN testing b ON b.retained = a.droppedWHERE NOT EXISTS (SELECT 1 FROM testing i WHERE a.retained = i.dropped)
drop table dbo.testingcreate table dbo.testing (retained int, dropped int)insert into dbo.testing (retained, dropped)select 972580 , 697688 union allselect 1354938, 697688 union allselect 1354938 , 972580 union allselect 1555243, 1354938