SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Calculate a running total?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Rasta Pickles
Posting Yak Master

United Kingdom
171 Posts

Posted - 01/14/2013 :  05:41:40  Show Profile  Reply with Quote
Hi,

I have 320,000 records in a temporary table that is created each time the query is executed.

There are five fields: value, acctno, yrmonth, division and centre

The table holds data from 2006 and I'd like to be able to calculate a running sum for each acctno based on value. When the acctno changes, the running total should reset to zero and start summing again.

Any hints would be greatly appreciated.

Many thanks, as always.

Edited by - Rasta Pickles on 01/14/2013 05:42:39

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/14/2013 :  05:45:13  Show Profile  Reply with Quote
see scenario 1

http://visakhm.blogspot.in/2010/01/multipurpose-apply-operator.html



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Rasta Pickles
Posting Yak Master

United Kingdom
171 Posts

Posted - 01/14/2013 :  06:07:56  Show Profile  Reply with Quote
Many thanks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/14/2013 :  06:27:45  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Jeff Moden
Aged Yak Warrior

USA
649 Posts

Posted - 01/14/2013 :  23:26:55  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

see scenario 1

http://visakhm.blogspot.in/2010/01/multipurpose-apply-operator.html



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Oh, be careful, now. That code contains a "Triangular Join" and, depending on the data, can be hundreds, thousands, and even millions of times worse than a cursor, while loop, or recursive CTE. Please see the following article for more on "Triangular Joins".

http://www.sqlservercentral.com/articles/T-SQL/61539/

--Jeff Moden
RBAR is pronounced "ree-bar" and is a "Modenism" for "Row By Agonizing Row".

First step towards the paradigm shift of writing Set Based code:
"Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

When writing schedules, keep the following in mind:
"If you want it real bad, that's the way you'll likely get it."
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000