Sum of certain group in a column

I have an excel where I need to find the sum like this. I’m stuck since some need to sum by batch while some doesn’t.

Here’s attached the input and output in one file:
Example.xlsx (12.8 KB)