Hello Madhu,
I think the logic has to be implemented in different way to resolve the issues listed.
Step 1: Create new output field Rank_New = decode(RANK = 1, 1, 2). Here, we are keeping the records other than rank 1 to new group
Step 2: Apply sort or SSN, Rank_New and then Rank
Step 3: Output of Step 2 is connected to 2 query transforms. No need to apply any filter.
Step 4: Join previous 2 query transforms (Inner join). Use below join criteria. Get SSN, Rank, Balance from Query_Current. Get Paid Amt from Query_Old
Query_Old.SSN = Query_Current.SSN AND
Query_Current.RANK_NEW= Query_Old.RANK_NEW AND
Query_Old.RANK <= Query_Current.RANK
Step 5: Aggregate Paid Amount. Sum(Paid_amt) and all other fields in group by clause
Step 6: New_balance = decode(rank = 1, balance, balance - paid_amt)
Step 7: Create new field for calculating balance, balance_inter = previous_row_value(New_balance)
Step 8: Balance = decode(rank = 1, balance, balance_inter)
Hope the above steps will resolve your issue. If you are getting incorrect values in new balance and balance, apply sort on ssn and rank at the step 5.