If this was me I would do something like this ... this is my test data
declare @TransactionMaster table (DOT date, TXN_TYPE varchar(3), CHQ_NO int null, TXN_AMOUNT bigint)
insert into @TransactionMaster (DOT, TXN_TYPE, CHQ_NO, TXN_AMOUNT) values
('2020-03-28', 'CQD', 50, 84790)
,('2020-04-01', 'CW', null, 59415)
,('2020-10-25', 'CQD', 12, 191785)
,('2021-02-12', 'CD', null, 125678)
,('2021-09-23', 'CW', 10, 15000);
I would use a CTE (or a temporary table, or a sub-query) to "pre-prepare" my data by multiplying by (-1) to make the CW values negative. Then I can use a very simple SUM ... OVER to calculate the running total e.g.
;with preprepare as
(
select
ROW_NUMBER() over (order by DOT asc) as RNO
,DOT
,TXN_TYPE
,CHQ_NO
,CASE WHEN TXN_TYPE = 'CW' THEN (-1) * TXN_AMOUNT ELSE TXN_AMOUNT END AS TXN_AMOUNT
from @TransactionMaster where datediff(YY,dot,getdate()) <= 2
)
select
RNO
,DOT
,TXN_TYPE
,CHQ_NO
,TXN_AMOUNT
,SUM(TXN_AMOUNT) OVER (ORDER BY RNO) as RUNNING_TOTAL
from preprepare;
Results:
RNO DOT TXN_TYPE CHQ_NO TXN_AMOUNT RUNNING_TOTAL
1 2020-03-28 CQD 50 84790 84790
2 2020-04-01 CW NULL -59415 25375
3 2020-10-25 CQD 12 191785 217160
4 2021-02-12 CD NULL 125678 342838
5 2021-09-23 CW 10 -15000 327838