Author |
Topic |
taniarto
Starting Member
27 Posts |
Posted - 2013-06-30 : 23:22:52
|
I have some data as example :ID Route ResultA001 Order 100B001 Proses 50C001 Complete 50I want to make resulting like this :ID Route Start Result TotalA001 0 100 100B001 100 50 150C001 150 50 200Pleas Help...Thanks |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2013-06-30 : 23:33:32
|
[code]select t.ID, t.Route, Start = r.Total - t.Result, t.Result, r.Totalfrom your_table t cross apply ( select Total = sum(Result) from your_table x where x.ID <= t.ID ) r[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
taniarto
Starting Member
27 Posts |
Posted - 2013-07-01 : 05:34:26
|
If there were contain group of calculation..what should the syntax is?table t contain several data and I want to make a group table x already a group..thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-01 : 06:07:26
|
quote: Originally posted by taniarto If there were contain group of calculation..what should the syntax is?table t contain several data and I want to make a group table x already a group..thanks
Sorry not clear on your scenarioPost some sample data and illustrate.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2013-07-01 : 07:39:56
|
quote: Originally posted by taniarto If there were contain group of calculation..what should the syntax is?table t contain several data and I want to make a group table x already a group..thanks
use CTE.; with cteas( your existing query)select t.ID, t.Route, Start = r.Total - t.Result, t.Result, r.Totalfrom cte t cross apply ( select Total = sum(Result) from cte x where x.ID <= t.ID ) r KH[spoiler]Time is always against us[/spoiler] |
|
|
taniarto
Starting Member
27 Posts |
Posted - 2013-07-05 : 02:44:39
|
If the result is running decrease what is the formula ?thanksquote: Originally posted by khtan
select t.ID, t.Route, Start = r.Total - t.Result, t.Result, r.Totalfrom your_table t cross apply ( select Total = sum(Result) from your_table x where x.ID <= t.ID ) r KH[spoiler]Time is always against us[/spoiler]
|
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2013-07-05 : 02:47:24
|
quote: If the result is running decrease what is the formula ?
Example ? KH[spoiler]Time is always against us[/spoiler] |
|
|
taniarto
Starting Member
27 Posts |
Posted - 2013-07-05 : 03:58:50
|
the example :ID Start Trans End001 100 30 70002 70 20 50003 50 10 40004 40 30 10thanksquote: Originally posted by khtan
quote: If the result is running decrease what is the formula ?
Example ? KH[spoiler]Time is always against us[/spoiler]
|
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2013-07-05 : 04:45:37
|
how does the original data looks like ?Where is the "Start" value comes from ? KH[spoiler]Time is always against us[/spoiler] |
|
|
taniarto
Starting Member
27 Posts |
Posted - 2013-07-05 : 05:25:18
|
The beginning data actually is zero (0)because the data start calculating on the trans field and then the end field. for the next process on the second record, the formula containing of start - TRans=Endsorry the example before is wrong. Please read the new oneID Start Trans End001 0 30 30002 30 20 10003 10 10 0 quote: Originally posted by khtan how does the original data looks like ?Where is the "Start" value comes from ? KH[spoiler]Time is always against us[/spoiler]
|
|
|
taniarto
Starting Member
27 Posts |
Posted - 2013-07-05 : 22:31:33
|
the original data :ID Route total001 Preparation 100002 Cutting 60003 Vernish 20004 Packing 10From the data I want to know how many the total left. which is come from calculation of the total. So it can result like this :ID Route start total End001 Preparation 0 100 100002 Cutting 100 60 40003 Vernish 40 20 20004 Packing 20 10 10So from the information I can Know my Last calculation on the last ID (on End Field)Thanksquote: Originally posted by taniarto The beginning data actually is zero (0)because the data start calculating on the trans field and then the end field. for the next process on the second record, the formula containing of start - TRans=Endsorry the example before is wrong. Please read the new oneID Start Trans End001 0 30 30002 30 20 10003 10 10 0 quote: Originally posted by khtan how does the original data looks like ?Where is the "Start" value comes from ? KH[spoiler]Time is always against us[/spoiler]
|
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2013-07-05 : 23:34:38
|
Assuming that ID 001 is the addition (positive) to the total and others ID are subtraction (-negative). Unless you have another column to indicate that.select t.ID, t.Route, Start = case when t.ID = '001' then 0 else r.[End] + t.Total end, t.Total, r.[End]from cte t cross apply ( select [End] = sum(case when x.ID = '001' then Total else -Total end) from cte x where x.ID <= t.ID ) r KH[spoiler]Time is always against us[/spoiler] |
|
|
taniarto
Starting Member
27 Posts |
Posted - 2013-07-15 : 04:19:08
|
sorry for asking again..IF the result like this what is the formula ?ID Route Receive total Result001 Order 0 100 100002 Process 100 40 60003 Vernish 40 10 30the receive field is coming from total field thx again |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2013-07-15 : 04:36:28
|
[code]cte2 as( select *, rn = row_number() over (order by ID) from cte)select t1.ID, t1.Route, Receive = isnull(t2.Total, 0), t1.Total, Result = isnull(t2.Total - t1.Total, t1.Total)from cte2 t1 left join cte2 t2 on t1.rn = t2.rn + 1[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
taniarto
Starting Member
27 Posts |
Posted - 2013-07-15 : 05:28:53
|
does it work on cross apply function? |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2013-07-15 : 05:50:42
|
addicted to the APPLY function ? it will need an OUTER rather than CROSS apply to workcte2 as( select *, rn = row_number() over (order by ID) from cte)select c.ID, c.Route, Receive = isnull(r.Receive, 0), c.Total, Result = isnull(r.Receive - c.Total, c.Total)from cte2 c outer apply ( select Receive = Total from cte2 x where x.rn = c.rn - 1 ) r KH[spoiler]Time is always against us[/spoiler] |
|
|
taniarto
Starting Member
27 Posts |
Posted - 2013-07-15 : 22:44:16
|
thanks mr.khtan.. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2013-07-15 : 22:46:49
|
welcome KH[spoiler]Time is always against us[/spoiler] |
|
|
|