Invoke Code for datatable

as you have already the data you can do it with a groupby on ids and summing up the amunts as described there:

Also check if it can be done directly on the datatabase with corresponding SQL, an option which one is also to prefer