Solved

Running Total & Count

Posted on 2001-09-12
5
370 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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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 69

Expert Comment

by:ScottPletcher
Comment Utility
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
Comment Utility
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
Comment Utility
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 69

Accepted Solution

by:
ScottPletcher earned 15 total points
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now