cumulative sum not working

I am trying to get the cumulative sum for an entire column. Below is a sample of my data.

DT                             ETB_DT
Friday 00:00:00.0000000 Friday 04:00:00.0000000
Friday 00:00:00.0000000 Friday 08:00:00.0000000
Friday 00:00:00.0000000 Friday 12:00:00.0000000
Friday 00:00:00.0000000 Friday 16:00:00.0000000
Friday 00:00:00.0000000 Friday 20:00:00.0000000

No_of_records   
22             
41  
86  
65  
25  

I have used the following code to get the cumulative column which is supposed to add on to the previous row.

  • SQL Server 2012 - Pass stored procedure name to another stored procedure
  • EXPRESSION - SQL Reporting Builder - Count & SUM to specific field
  • Stored Procedures with arguments
  • Use SqlDataReader and string array
  • Xquery to return all nodes defined in the outer apply not just
  • SQL Server: Arithmetic overflow error converting expression to data type bigint
  • Select *
           ,cumulative = sum(No_of_records) 
     From  withoutcumulative4
     group by DT,ETB_DT,No_of_records
      Order by DT
    

    However, this gives me the following output.

    DT                             ETB_DT
    Friday 00:00:00.0000000 Friday 04:00:00.0000000
    Friday 00:00:00.0000000 Friday 08:00:00.0000000
    Friday 00:00:00.0000000 Friday 12:00:00.0000000
    Friday 00:00:00.0000000 Friday 16:00:00.0000000
    Friday 00:00:00.0000000 Friday 20:00:00.0000000
    
    No_of_records   cumulative
    22                  22
    41                  41
    86                  86
    65                  65
    25                  25
    

    Below is my desired output.

    DT                             ETB_DT
    Friday 00:00:00.0000000 Friday 04:00:00.0000000
    Friday 00:00:00.0000000 Friday 08:00:00.0000000
    Friday 00:00:00.0000000 Friday 12:00:00.0000000
    Friday 00:00:00.0000000 Friday 16:00:00.0000000
    Friday 00:00:00.0000000 Friday 20:00:00.0000000
    
    No_of_records   cumulative
    22                  22
    41                  63
    86                  149
    65                  214
    25                  239
    

    Do any of you have ideas? Really appreciate your help.

    2 Solutions collect form web for “cumulative sum not working”

    This is the syntax for cumulative sum:

    Select DT, ETB_DT, No_of_records
           sum(No_of_records) over (order by DT, ETB_DT) as cumulative
    From withoutcumulative4
    Order by DT;
    

    The over clause with order by is what makes the sum cumulative. Not assigning it a name.

    From what I can tell, you don’t need group by.

    you can use over:

    Select *
           ,cumulative = sum(No_of_records) over (order by ETB_DT) 
     From  withoutcumulative4
      Order by DT
    
    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.