Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Qnatz
Starting Member
4 Posts |
Posted - 2011-06-10 : 17:16:24
|
| Hey guysgot a Sql db n tblReport which looks like thisID Name StockIn StockOut Bal2 OP 100 0 1003 na 400 0 4 ma 0 6005 xz 0 40 6 vc 20 0i want to use sql to get this ID Name StockIn StockOut Bal2 OP 100 0 1003 na 400 0 5004 ma 0 600 -1005 xz 0 40 -1406 vc 20 0 -120i want to go by row in a looplikefor i = 0 to maxRow - 1Bal(i+1) = Bal(i) + StockIn(i)-StockOut(i)next ican anyone help pleaseQnatzQnatz |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-06-10 : 18:06:31
|
If you are on SQL 2005 or higher, a recursive CTE can do this for you. I am assuming that the ID's are numbered consecutively.;with a as( select Id, name, StockIn, Stockout, StockIn - StockOut as Bal from tblReport where Id = 2 -- or whatever is the starting Id union all select t.Id, t.Name, t.StockIn, t.StockOut, a.Bal - t.Stockout as Bal from tblReport t inner join a on t.Id = a.Id+1)select * from a; |
 |
|
|
Qnatz
Starting Member
4 Posts |
Posted - 2011-06-11 : 08:36:08
|
Thanks for the quick reply Sunitabeck. I do appreciate..LOTS.The code works perfect...but if forgot to say that a number of Openning balances(OP) will appear for each catgegory of crops. Name Crop StockIn StockOut BalOpeningBal Maize 15400 0 15400Tony Maize 5000 0 20400ceaser Maize 5000 10 25390Ek Maize 5400 100 30690sam Maize 4500 150 35040Tony Maize 4500 150 39390sam Maize 6000 0 45390OpeningBal Beans 5000 0 5000ceaser Beans 0 1000 4000Ek Beans 0 1000 3000sam Beans 50000 0 50000Tony Beans 0 4000 46000sam Beans 0 5000 41000Tony Beans 1000 2000 40000OpeningBal Soybean 60400 0 60400Ek Soybean 0 1000 59400sam Soybean 0 1000 58400Tony Soybean 0 2000 56400sam Soybean 5440 0 61840Tony Soybean 0 4000 57840ceaser Soybean 0 4000 53840how can i always go back to changeId = 2 -- or whatever is the starting Idwhen ever OP is foundthanksQnatz |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-06-11 : 08:59:52
|
| In your new sample data, there is one more piece of information that is required. And that is the order in which the Stock is consumed. This is required because, by definition, rows in a table are unordered. So even if you inserted the data in a specific order, SQL has no knowledge of that order, unless you specify it. In your previous posting, you had an ID column which indicated the order. So we were able to make use of that. Do you have something similar - a sequential id number, or a date/time column or anything else that would help us determine the ordering? |
 |
|
|
Qnatz
Starting Member
4 Posts |
Posted - 2011-06-11 : 15:43:33
|
| VERY SORRY...the table has an ID column that starts always at 1ID Name Crop StockIn StockOut Bal1 OpeningBal Maize 15400 0 154002 Tony Maize 5000 0 204003 ceaser Maize 5000 10 253904 Ek Maize 5400 100 306905 sam Maize 4500 150 350406 Tony Maize 4500 150 393907 sam Maize 6000 0 453908 OpeningBal Beans 5000 0 50009 ceaser Beans 0 1000 400010 Ek Beans 0 1000 300011 sam Beans 50000 0 5000012 Tony Beans 0 4000 4600013 sam Beans 0 5000 4100014 Tony Beans 1000 2000 4000015 OpeningBal Soybean 60400 0 6040016 Ek Soybean 0 1000 5940017 sam Soybean 0 1000 5840018 Tony Soybean 0 2000 5640019 sam Soybean 5440 0 6184020 Tony Soybean 0 4000 5784021 ceaser Soybean 0 4000 53840thanks again for your help.Much appreciatedQnatzQnatz |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-06-11 : 16:14:28
|
Thanks, that makes it easier! See if the query below gives you the results you are looking for.;with cte as( select *,row_number() over (partition by Crop order by ID) N from tblReport),a as( select Id, name, Crop, StockIn, Stockout, StockIn - StockOut as CalculatedBal from cte where N = 1 union all select t.Id, t.Name, t.Crop, t.StockIn, t.StockOut, a.CalculatedBal + t.StockIn - t.Stockout as CalculatedBalBal from cte t inner join a on t.Id = a.Id+1 and a.Crop = t.Crop)select * from a order by Id; |
 |
|
|
Qnatz
Starting Member
4 Posts |
Posted - 2011-06-11 : 17:14:06
|
| if GOD, Jesus n Muhammed were not in the picture.....YO GOD.Just worked perfect...after 4 months of hassling.......Just off topic alito....could you help me n advise me on how to be perfect...i do alot of stuff,,, i graduated with a BSc Agric am a farmeri teaching farming as well as market for my company.... i also headthe IT dept for which am doin this app.Am a grafix consult who does movies, directing n production worksbut am not so perfect in every thing i do....when i do some alot of people are mouth dropped but i feel there's some always missin....how can i be contented with my self coz fewguys here can do stuff i do..... actually i also do part time lecturing to IT students especially on their course works...Just some advise...like a parent...i missed that.Thanx a bunch BROQnatz |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-06-11 : 19:40:53
|
There is an error in the code that I had posted earlier - see changes below. That it worked for the sample data was just a coincidence.;with cte as( select *,row_number() over (partition by Crop order by ID) N from tblReport),a as( select Id, name, Crop, StockIn, Stockout, StockIn - StockOut as CalculatedBal,N from cte where N = 1 union all select t.Id, t.Name, t.Crop, t.StockIn, t.StockOut, a.CalculatedBal + t.StockIn - t.Stockout as CalculatedBal, t.N from cte t inner join a on t.N = a.N+1 and a.Crop = t.Crop)select * from a order by Id; As for advice on becoming perfect, I am very unqualified to offer any such advice. If you like to improve your Database/SQL skills, start another thread asking for advice. I am sure many people on this forum who have far more experience and skills than I, would offer very useful suggestions. |
 |
|
|
|
|
|
|
|