Ok, I think I found the issue, but don't know how to change this to fix it.
The problem seems to be with this code:
Select S1.*, S2.Cnt , rn = ROW_NUMBER() OVER (PARTITION BY InvAmt_Sum ORDER BY batNbr DESC)
specifically this:
rn = ROW_NUMBER() OVER (PARTITION BY InvAmt_Sum ORDER BY batNbr DESC)
I think this is creating an issue. The rn should count incrementally by VndNbr, InvNbr.