devxlogo

Handling Cumulative Totals in T-SQL

Handling Cumulative Totals in T-SQL

Suppose you have a table:

 select * from tblTestName	Amount---	------A	10B	20c	40D	50E	30

The following single SELECT query can be used to calculate cumulative totals:

 --//////////////////////////////////////SELECT t1.Name, t1.Amount,(SELECT SUM(t2.Amount ) FROM tblTest AS t2 WHERE t2.Name <= t1.Name ) ASTotalFROM tblTest AS t1--//////////////////////////////////////Name	Amount	Total---	------	-----A	10	10B	20	30c	40	70D	50	120E	30	150
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist