Calculating a new value based on the value of a prior row

Hello,

I have a table set up similar to the on below:

Date| Starting Balance | Quantity Received | Quantity Used | New Balance

The new balance is the starting balance for the next row.

So for Row 1... The formula is

Starting Balance + Quantity Received - Quantity Used = New Balance

For Row 2 to End for each part it is

Previous Lines New Balance + Quantity Received - Quantity Used = New Balance

I think you can do this by adding a rank row and some Case Statements but I am getting my self wrapped around the Axle.  

Any help will be appreciated.

Mike
Mike DeFeliceAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nakul VachhrajaniTechnical Architect, Capgemini IndiaCommented:
Assuming that this is SQL Server 2012 and above, you can use the ORDER BY clause of windowing/ranking functions. So, you are right on target about the tentative solution.

I wrote a post about using ranking functions to do a running total. You can leverage the same solution by modifying the formula to refer to your fields.

My post is available for your reference at: https://nakulvachhrajani.com/2014/04/03/0330-sql-server-2012-running-total-problem-and-windowing-functions/
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike DeFeliceAuthor Commented:
Thank you!  This worked great!
0
Nakul VachhrajaniTechnical Architect, Capgemini IndiaCommented:
Good  to know that my post helped in resolving your issue! Request you to please close this question in case there are no follow-up queries.
0
Mike DeFeliceAuthor Commented:
I assigned you as the best solution.  Is there anything else I have to do to close it?
0
Nakul VachhrajaniTechnical Architect, Capgemini IndiaCommented:
Hello!

You may need to click on "Next"  and then close the question (http://support.experts-exchange.com/customer/en/portal/articles/2527982-how-do-i-close-my-question-).

Thanks! :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.