You can do this using the SUMIFS() function. Here is an example. Pls pay attention to the absolute references in the formula, i.e. the "$" signs; they are necessary for the formula to work correctly when you copy & paste it to other cells.

[![enter image description here][1]][1]

  [1]: https://i.sstatic.net/e8CAN.png