SELECT [ProdDate], Receipt, Payment, @OpeningBal+ Receipt-Payment + COALESCE((SELECT SUM(Receipt-Payment) FROM @tab AS s WHERE s.ProdDate < o.ProdDate and ProdDate between @FromDate and @ToDate), 0) As Total FROM @tab AS oWHERE ProdDate between @FromDate and @ToDateORDER BY ProdDate

SELECT Date, Recptamt, Payamt, @OpeningBal + Recptamt-Payamt + COALESCE((SELECT SUM(Recptamt-Payamt) FROM tbltest1 AS s WHERE s.Date < o.Date and Date between _FromDate and _ToDate), 0) As Total , @OpeningBal as OpeningBalFROM tbltest1 AS oWHERE Date between @FromDate and @ToDate and shareno=_sharenoORDER BY Date;END