I think that the solution sica offered is close, but might run into problems if test datasets had more than two distinct amounts(disclaimer: I could be wrong about this, so try his solution first --it is definitely a lot more straight forward than mine). Here is my cut at it:--setup test datacreate table table1(id varchar(5), amt int)create table table2(id varchar(5), amt int)insert into table1 values ('01', 44)insert into table1 values ('02', 100)insert into table1 values ('03', 100)insert into table2 values ('41', 100)insert into table2 values ('42', 100)insert into table2 values ('43', 100)go-- create temp tables (to hold ids that will not be deleted)create table #t1_keepers (id varchar(5))create table #t2_keepers (id varchar(5))-- Figure out ids to keep in table1insert into #t1_keepersselect src.id from (select COUNT(*) as rank, table1.id, table1.amt from table1 INNER JOIN table1 r ON (table1.amt = r.amt AND table1.id <= r.id)group by table1.id, table1.amt) as src INNER JOIN (select a.amt, CASE WHEN (a.cnt - isnull(b.cnt, 0)) < = 0 THEN 0 ELSE (a.cnt - isnull(b.cnt, 0)) END as num_to_keepFROM (select amt, count(*) as cnt from table1group by amt) as aLEFT JOIN (select amt, count(*) as cnt from table2group by amt) b ON (a.amt = b.amt)) as qualON (src.amt = qual.amt)WHERE (src.rank <= qual.num_to_keep )-- Figure out ids to keep in table2insert into #t2_keepersselect src.id from (select COUNT(*) as rank, table2.id, table2.amt from table2 INNER JOIN table2 r ON (table2.amt = r.amt AND table2.id <= r.id)group by table2.id, table2.amt) as src INNER JOIN (select a.amt, CASE WHEN (a.cnt - isnull(b.cnt, 0)) < = 0 THEN 0 ELSE (a.cnt - isnull(b.cnt, 0)) END as num_to_keepFROM (select amt, count(*) as cnt from table2group by amt) as aLEFT JOIN (select amt, count(*) as cnt from table1group by amt) b ON (a.amt = b.amt)) as qualON (src.amt = qual.amt)WHERE (src.rank <= qual.num_to_keep)--Delete unwanted recordsDELETE table1 FROM table1 t1left join #t1_keepers as t1k on (t1.id = t1k.id)WHERE t1k.id IS NULL--Delete unwanted recordsDELETE table2 FROM table2 t2left join #t2_keepers as t2k on (t2.id = t2k.id)WHERE t2k.id IS NULLselect * from table1select * from table2drop table #t1_keepersdrop table #t2_keepers