Correct sum_by_entity function.