I want to return the maximum value for a certain thing dependant on to variables. An example works best:

Lets say that I would like to find the maximum rating of x so long as it fits category a

x 1 a
y 3 a
z 2 b
y 1 b

x 2 a
z 2 a
x 3 b

In this case I want the value of 2 returned for x as this is the highest value for x as long as it is also a. I don't want the red ones as they are not x nor do I want the yellow ones as it is not a. This leaves two which I want the highest which would then be two. It may be a simple problem but I cannot find a way of doing it without an error and would appreciate it if someone could find a formula that works.