Apparently, by the model you presented, you want to store the number of transactions with different initial codes happen ...
In this case, when performing a FOR EACH store this code in a variable, then compare it with the next record, if it is the same ignore the count, if it is different add +1...