Running Total is not calculating correctly on Date Filter?

Analyst_SQL 3,576 Reputation points
2021-10-11T01:57:41.597+00:00

When i am filtering data on date filter then running total is not calculating correctly.

Create table #tbl_Student (S_ID int,S_Name varchar(50))  
 create table #tbl_Fees (F_ID int,F_Date date,S_ID int,F_amt int)  
 create table #tbl_Receive(R_ID int,R_Date date,S_ID int,R_Amt int)  
      
      
 insert into  #tbl_Student values (1001,'AKhter');  
 insert into #tbl_Fees values(1,'2021-10-01',1001,5000)  
 insert into #tbl_Fees values(2,'2021-10-01',1001,5000)  
 insert into #tbl_Fees values(3,'2021-10-03',1001,5000)  
 insert into #tbl_Fees values(4,'2021-10-06',1001,5000)  
      
      
 Insert into #tbl_Receive values(1, '2021-10-02',1001,8000)  
 Insert into #tbl_Receive values(2, '2021-10-03',1001,4000)  
 Insert into #tbl_Receive values(3, '2021-10-05',1001,2000)  
   
   
 ;With cte As  
 (Select f.F_ID As FRID, f.F_Date As Date, s.S_Name, f.F_Amt, 0 As R_Amt, 'F' As Type  
 From #tbl_Student s  
 Inner Join #tbl_Fees f On s.S_ID = f.S_ID  
 Union All  
 Select r.R_ID As FRID, r.R_Date As Date, s.S_Name, 0 As F_Amt, r.R_Amt, 'R' As Type  
 From #tbl_Student s  
 Inner Join #tbl_Receive r On s.S_ID = r.S_ID)  
 Select FRID As [F_ID/R_ID], S_Name, Date As S_Date, F_amt As S_Fee,  
    R_Amt,  
    Sum(F_Amt - R_Amt) Over(Partition By S_Name Order By Date, Type, FRID) As Running_Total  
 From cte  where Date between '2021-10-02' and '2021-10-05'  
 Order By S_Name, S_Date, Type, [F_ID/R_ID];  

Currently Output ,which is wrong
139168-image.png

Correct ouput

139216-image.png

Developer technologies | Transact-SQL
SQL Server | Other
{count} votes

Accepted answer
  1. EchoLiu-MSFT 14,621 Reputation points
    2021-10-11T06:24:56.863+00:00

    Please check:

     ;With cte As
     (Select f.F_ID As FRID, f.F_Date As Date, s.S_Name, f.F_Amt, 0 As R_Amt, 'F' As Type
     From #tbl_Student s
     Inner Join #tbl_Fees f On s.S_ID = f.S_ID
     Union All
     Select r.R_ID As FRID, r.R_Date As Date, s.S_Name, 0 As F_Amt, r.R_Amt, 'R' As Type
     From #tbl_Student s
     Inner Join #tbl_Receive r On s.S_ID = r.S_ID)
     ,cte2 as(Select FRID As [F_ID/R_ID], S_Name, Date As S_Date, F_amt As S_Fee,
        R_Amt,Type,
        Sum(F_Amt - R_Amt) Over(Partition By S_Name Order By Date, Type, FRID) As Running_Total
     From cte)
    
     SELECT * FROM cte2
     WHERE S_Date between '2021-10-02' and '2021-10-06'
     Order By S_Name, S_Date, Type, [F_ID/R_ID];
    

    If you have any question, please feel free to let me know.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Tom Phillips 17,771 Reputation points
    2021-10-11T15:31:36.107+00:00

    Your problem is not the running total is not working, it is working perfectly.

    The problem is you need to calculate the starting balance before 10/2/2021 to start the process.

    The simplest answer to your problem is to calculate the running total for all records, and only DISPLAY the date range in question.

    ;With cte As
      (Select f.F_ID As FRID, f.F_Date As Date, s.S_Name, f.F_Amt, 0 As R_Amt, 'F' As Type
      From #tbl_Student s
      Inner Join #tbl_Fees f On s.S_ID = f.S_ID
      Union All
      Select r.R_ID As FRID, r.R_Date As Date, s.S_Name, 0 As F_Amt, r.R_Amt, 'R' As Type
      From #tbl_Student s
      Inner Join #tbl_Receive r On s.S_ID = r.S_ID)
    , final as (
      Select FRID As [F_ID/R_ID], S_Name, Date As S_Date, F_amt As S_Fee,
         R_Amt,Type, FRID,
         Sum(F_Amt - R_Amt) Over(Partition By S_Name Order By Date, Type, FRID) As Running_Total
      From cte  
    )
    SELECT *
    FROM final 
    where S_Date between '2021-10-02' and '2021-10-05'
    Order By S_Name, S_Date, Type, [F_ID/R_ID], FRID
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.