i have this table tbl_CustJournals
J_Id Date Num Type custId Debit Credit Net
1 17/01/2018 5000 Invoice 2503 1472.78 0 1472.78
2 17/01/2018 1 Pay 2503 0 980 -980
3 07/02/2018 5 Invoice 2503 318.8 0 318.8
4 17/01/2018 2 Pay 2503 0 492 -492
5 09/02/2018 5 Invoice 270 4490 0 4490
6 07/02/2018 3 Pay 270 0 900 -900
7 10/02/2018 55 Invoice 65 14000 0 14000
8 17/01/2018 23 Invoice 65 520 0 520
9 17/01/2018 24 Invoice 65 525 0 525
10 17/01/2018 24 Invoice 65 1400 0 1400
11 17/01/2018 25 Invoice 65 1400 0 1400
12 17/01/2018 25 Invoice 65 975 0 975
13 17/01/2018 26 Invoice 65 3310 0 3310
14 17/01/2018 252 Invoice 65 3708.1 0 3708.1
15 04/02/2018 27 Invoice 2503 14000 0 14000
16 04/02/2018 28 Invoice 2503 14000 0 14000
17 17/02/2018 29 Invoice 65 640 0 640
18 07/02/2018 4 Pay 2503 0 999 -999
19 07/02/2018 4 Pay 2503 0 900 -900
20 07/02/2018 4 Pay 2503 0 101 -101
21 07/02/2018 4 Pay 2503 0 900 -900
22 17/01/2018 78 Invoice 65 180.99 0 180.99
23 17/01/2018 78 Invoice 65 231 0 231
i want to have A running total
What I have tried:
i tried this Procedure
ALTER procedure [dbo].[SP_Customer_Get_Balnance_By_Declare_Table]
@custcode int,
@datefrom date,
@dateto date
as
DECLARE @st TABLE
(
[Date] DATE ,
J_Number int,
J_Type varchar(25),
[Description] varchar(max),
Debit money,
Credit money,
Gl_Debits money,
Net_Balance money
);
DECLARE @NetBalance money = 0;
INSERT @st([Date],J_Number,J_Type,[Description],Debit,Credit, Gl_Debits, Net_Balance)
SELECT Journal_Date,Journal_Num,Journal_Type,[Description],Debit,Credit, GL_Debits, RunningTotal = 0
FROM dbo.Tbl_Cust_Journals
where Tbl_Cust_Journals.custId =@custcode and Journal_Date between @datefrom and @dateto
ORDER BY Journal_Date;
UPDATE @st
SET @NetBalance = Net_Balance = @NetBalance + Gl_Debits
FROM @st;
SELECT [Date] ,J_Number ,J_Type ,[Description] ,Debit ,Credit , Gl_Debits , Net_Balance
FROM @st
ORDER BY [Date];
return
but the problem is when the custid is 2503 and the date is between "04/02/2018" and "07/02/2018" i want to have an oppining balance
can any one help me