select sum(C) from (select A,max(B) B from table group by A) t1 , table t2 where t1.A = t2.A and t1.B = t2.B