;with adj as
(
select c.pk,
amount_paid = a.amount_paid - c.amount,
tax_paid = a.tax_paid - c.tax_value,
etax_paid = a.etax_paid - c.etax_vale
from charge_table c
inner join
(
select pk,
amount_paid = sum(amount_paid),
tax_paid = sum(tax_paid),
etax_paid = sum(etax_paid)
from another_table
group by pk
) a on c.pk = a.pk
)
update c
set amount_paid = c.amount_paid - a.amount_paid,
tax_paid = c.tax_paid - a.tax_paid,
etax_paid = c.etax_paid - a.etax_paid
from (
select *, rn = row_number() over( partition by pk order by amount_paid )
from another_table
) c
inner join adj a on c.pk = a.pk
where c.rn = 1
KH
Time is always against us