I agree with oeldere. A sample file is very helpful in getting solutions.

In the mean time. Try working with the SUMIFS function.

Hope this is helpful.