This script works for smaller tables - up to a few hundred records. It can be slow for thousands of records.
Select
Doc, TranDate, AmtIn, AmtOut,
Balance=(Select sum(AmtIn-AmtOut) From table1 Where Doc<=t.doc And TranDate<=t.TranDate)
From table1 As t
The next script will be faster for larger numbers of records.
Create table #tmp (Doc int, TranDate datetime, AmtIn int, AmtOut int, Balance int)
Insert #tmp
Select Doc, TranDate, AmtIn, AmtOut, 0
From #table1
Declare @balance int
set @balance=0
Update #tmp
Set @balance=Balance=@balance+AmtIn-AmtOut
Select * From #tmp
Drop table #tmp Terry L. Broadbent
FAQ183-874 contains tips for posting questions in these forums.
NOTE: Reference to the FAQ is not directed at any individual.