Library code snippets

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

Comments

  1. 04 Jul 2003 at 02:09

    What about performance?  Any performance issue in term of querying a large number of data?
    Any help is appreciated!

  2. 26 Nov 2002 at 12:24
    Very nice! very helpful. Thanks.
  3. 01 Jan 1999 at 00:00

    This thread is for discussions of SQL Running Total.

Leave a comment

Sign in or Join us (it's free).

 neilc

Related podcasts

  • Rocky Lhotka on Data Access Mania, LINQ and CSLA.NET

    Scott talks with developer and author Rockford Lhotka about the attack of the DALs (Data Access Layers). How can we put LINQ to SQL, LINQ to Entities and classic multi-tiered design all into a larger context? What's the right strategy for your data access needs? Scott's got questions and Rocky's ...

We'd love to hear what you think! Submit ideas or give us feedback