| Author |
Topic |
|
Lurmis
Starting Member
4 Posts |
Posted - 2009-02-11 : 11:59:47
|
| I have a need to add the value of one field to the value of the previous field in the same column, and continue adding each value until a limit is reached. Example: With a limit of 60Returned Data would look like this.RequiredQty | RequiredDate | CumulativeTotal | |12 | 2009-01-21 | 1215 | 2009-01-28 | 2720 | 2009-02-12 | 4710 | 2009-02-20 | 57I need to create and calculate the column 'CumulativeTotal'Any suggestions? |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-11 : 12:11:43
|
In SQL 2000Select RequiredQty,RequiredDate,(Select Sum(RequiredQty) from Yourtable Where RequiredQty <= T.RequiredQty)as CumulativeTotalfrom yourTable T |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-11 : 12:20:20
|
| [code]--Create TVDeclare @T table(RequiredQty int, RequiredDate datetime)Insert @TSelect 12,'2009-01-21' union allSelect 15,'2009-01-28' union allSelect 20,'2009-02-12'--Roll SQL EngineSelect t.RequiredQty,t.RequiredDate,m.CumulativeTotalfrom @T tOUTER APPLY(select Sum(RequiredQty) as CumulativeTotal from @T Where RequiredQty <= t.RequiredQty)m[/code] |
 |
|
|
Lurmis
Starting Member
4 Posts |
Posted - 2009-02-11 : 13:17:15
|
What does the union all do when inserting the values into @T?The RequiredQty and RequiredDate are variablesquote: Originally posted by sodeep
--Create TVDeclare @T table(RequiredQty int, RequiredDate datetime)Insert @TSelect 12,'2009-01-21' union allSelect 15,'2009-01-28' union allSelect 20,'2009-02-12'--Roll SQL EngineSelect t.RequiredQty,t.RequiredDate,m.CumulativeTotalfrom @T tOUTER APPLY(select Sum(RequiredQty) as CumulativeTotal from @T Where RequiredQty <= t.RequiredQty)m
|
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-11 : 13:46:48
|
| Did it work? It insert all values including duplicates. |
 |
|
|
Lurmis
Starting Member
4 Posts |
Posted - 2009-02-11 : 14:44:23
|
| Not really. Basically what I am trying to do is figure out when an inventory item will be depleted. Say I have 50 pcs of item XYZ. If there are 3 orders out there with demands of 20, 25, and 35; the 3rd order will be short unless we order more of item XYZ. I have a query that returns the demands ordered by required date, ascending. I am trying to get a query that will tell me what required date will be the one on which I will run short of parts. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-11 : 14:46:27
|
quote: Originally posted by Lurmis Not really. Basically what I am trying to do is figure out when an inventory item will be depleted. Say I have 50 pcs of item XYZ. If there are 3 orders out there with demands of 20, 25, and 35; the 3rd order will be short unless we order more of item XYZ. I have a query that returns the demands ordered by required date, ascending. I am trying to get a query that will tell me what required date will be the one on which I will run short of parts.
But it was not explained in previous post. Can we get sample data and expected output? |
 |
|
|
Lurmis
Starting Member
4 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-02-11 : 17:05:50
|
Well, he is wrong in on at least two things.I posted a comment for him to change his article. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|