2
Answers

Can you please help me with query

Naresh N

Naresh N

2y
685
1

input:

output what i want is:

Answers (2)
1
Rajanikant Hawaldar

Rajanikant Hawaldar

32 38.8k 451.4k 2y

Refer this optimized query

with cte as (
      SELECT *,
      row_number() over (partition by accountno order by trandate desc) as rn
      FROM test
      )
select accountno,
       trandate,
       amount,
       paidamount,
       balance,
       coalesce(balance + (select sum(balance) from cte a where a,rn > b.rn
       and a.accountno = b.accountno ), 0) as previous_balance
from
cte b
order by b.accountno, b.trandate
Accepted
1
Amit Mohanty

Amit Mohanty

17 52.2k 6.1m 2y

Try this

SELECT `Accountno`
      ,`Trandate`
      ,`Amount`
      ,`Paidamount`
      ,`Balance`
	  , (SELECT SUM(`Balance`) 
		   FROM `YourTableName` 
		  WHERE `Trandate` <= T1.`Trandate` 
			AND `Accountno` = T1.`Accountno`) INTO `Prevbalance`
  FROM `YourTableName` T1