January 14, 2014 at 7:08 am
Hi Experts,
I have a doubt.I have a table like this
I have a table with Purchase type column, amt spend column and currBalance column. Now my currbalance column should reflect the current balance amount.
I need currBalance column must have the current value after subtracting AmtSpend values of previous rows.
Kindly help me to achieve this...
Initially I have 10000 dollars as initial balance. My table structure is as follows.
ID TypeofPurchase AmtSpend CurrBalance
1 Half Pant 500 9500
2 Full Pant 800 8700
3 Half Shirt 1000 7700
4 Full Shirt 900 6800
5 Food 800 6000
6 Other exp 400 5600
January 14, 2014 at 7:39 am
What you have here is a running total problem. This is reasonably easy to solve using a "quirky update". You can read about them here. http://www.sqlservercentral.com/articles/T-SQL/68467/%5B/url%5D
Please make sure you very carefully read this article. There are some extremely important criteria for this to work correctly.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 2 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply