try playing around with sumif() and sumifs(), see if that gives you what you need