Hi @Kumar ,
Welcome to Microsoft Q&A!
Please refer to below:
select AcctNo,AsOfDate,PreviousDayBal,TransAmount,Credit_Debit
, PreviousDayBal + sum(case when Credit_Debit='Debit' then TransAmount*-1 else TransAmount end) OVER (partition by AcctNo ORDER BY rn ROWS UNBOUNDED PRECEDING) RunningBalance
from (
select AcctNo,AsOfDate,PreviousDayBal,TransAmount
,Credit_Debit,ROW_NUMBER() over (partition by AcctNo order by Credit_Debit) rn
from #Temp) a
order by rn
Output:
AcctNo AsOfDate PreviousDayBal TransAmount Credit_Debit RunningBalance
150 2021-04-29 100.00 20.00 Credit 120.00
150 2021-04-29 100.00 60.00 Credit 180.00
150 2021-04-29 100.00 30.00 Debit 150.00
150 2021-04-29 100.00 15.00 Debit 135.00
Best regards,
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.