Solved

Running Total & Count

Posted on 2001-09-12
5
372 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]
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 69

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 69

Accepted Solution

by:
Scott Pletcher earned 15 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help on t-sql 2012 10 54
Sql Stored Procedure 65 26
Simple SQL query from two tables 13 54
How to use three values with DATEDIFF 3 20
I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

776 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