[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Running Total & Count

Posted on 2001-09-12
5
Medium Priority
?
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

650 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