?
Solved

Running Total & Count

Posted on 2001-09-12
5
Medium Priority
?
380 Views
Last Modified: 2012-08-13
Is there a way that I could keep a Running Total & a Running Count?

For Example:
Running Count.
Total of 8 Records..
1,2,3,4,5,6,7,8

Running Total.
1 - 1
10 - 11
20 - 31
100 - 131
0
Comment
Question by:vbjohn
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6477009
If the records have primary key like this:

RecordID  Value
1         1
2         10
3         20
4         100
aso

Then:

SELECT
  R.RecordID,
  R.Value,
  TunningTotal = (SELECT SUM(T.VALUE) FROM YourTable T WHERE T.RecordID <= R.RecordID )
FROM YourTable R

Cheers

0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 6477425
On a large table, you are probably better off using a CURSOR for table access and calculating the running count and running total with other code.  I know that CURSORs should generally be avoided but in a case like this I see no acceptable alternative.
0
 

Author Comment

by:vbjohn
ID: 6477683
So how would I do that?  With the Cursors.  I have no experience with Stored Procedures.  Any coding examples please let me know.
0
 
LVL 9

Expert Comment

by:miron
ID: 6478463
there is a function, a really simple one, that I regularly forgot, and than re - create it again, when I have time, it is arithmetic progression. Right now I am between forgot and  re - create :).
If your increment is predictable, and you expect that rows will not be deleted some time in the future, performance and code will benefit of using this function instead of really summing up the values.
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 45 total points
ID: 6480233
Here is a sample of a method using a cursor:

CREATE @cursor1 CURSOR FOR
SELECT Value FROM YourTable
DECLARE @Value INT, @RecordId INT, @RunningTotal INT
SET @RecordId = 0
SET @RunningTotal = 0
OPEN @cursor1

FETCH NEXT FROM @cursor1 INTO @value
WHILE @@FETCH_STATUS  = 0
    BEGIN
    SET @RecordId = @RecordId + 1
    SET @RunningTotal = @RunningTotal + @Value
    SELECT @RecordId, @Value, @RunningTotal
    END
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question