SQL Running Total

I saw the running total example with the cursor and so on. Here is ONE simple single SQL statement(with a inline SELECT) which calculates the running total in Access OR MSSQL. No cursors required, only 6 lines of code, VERY fast and VERY simple. HERE GOES...

SELECT pro.ProductID, pro.ProductName, pro.UnitPrice,
   (SELECT SUM(pro1.UnitPrice)
       FROM Products pro1
       WHERE pro1.ProductID <= pro.ProductID) AS RuningTotal
   FROM Products pro
   ORDER BY pro.ProductID

You might also like...

Comments

 Neil

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” - Martin Fowler