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.

 All Forums
 SQL Server 2005 Forums
 Other SQL Server Topics (2005)
 Running Total

Author  Topic 

taniarto
Starting Member

27 Posts

Posted - 2013-06-30 : 23:22:52
I have some data as example :
ID Route Result
A001 Order 100
B001 Proses 50
C001 Complete 50

I want to make resulting like this :

ID Route Start Result Total
A001 0 100 100
B001 100 50 150
C001 150 50 200

Pleas 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.Total
from 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]

Go to Top of Page

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
Go to Top of Page

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 scenario
Post some sample data and illustrate.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

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 cte
as
(
your existing query
)
select t.ID, t.Route, Start = r.Total - t.Result, t.Result, r.Total
from 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]

Go to Top of Page

taniarto
Starting Member

27 Posts

Posted - 2013-07-05 : 02:44:39
If the result is running decrease what is the formula ?

thanks
quote:
Originally posted by khtan


select t.ID, t.Route, Start = r.Total - t.Result, t.Result, r.Total
from 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]



Go to Top of Page

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]

Go to Top of Page

taniarto
Starting Member

27 Posts

Posted - 2013-07-05 : 03:58:50
the example :

ID Start Trans End
001 100 30 70
002 70 20 50
003 50 10 40
004 40 30 10

thanks
quote:
Originally posted by khtan

quote:
If the result is running decrease what is the formula ?

Example ?


KH
[spoiler]Time is always against us[/spoiler]



Go to Top of Page

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]

Go to Top of Page

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=End
sorry the example before is wrong. Please read the new one
ID Start Trans End
001 0 30 30
002 30 20 10
003 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]



Go to Top of Page

taniarto
Starting Member

27 Posts

Posted - 2013-07-05 : 22:31:33
the original data :
ID Route total
001 Preparation 100
002 Cutting 60
003 Vernish 20
004 Packing 10

From 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 End
001 Preparation 0 100 100
002 Cutting 100 60 40
003 Vernish 40 20 20
004 Packing 20 10 10

So from the information I can Know my Last calculation on the last ID (on End Field)

Thanks

quote:
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=End
sorry the example before is wrong. Please read the new one
ID Start Trans End
001 0 30 30
002 30 20 10
003 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]





Go to Top of Page

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]

Go to Top of Page

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 Result
001 Order 0 100 100
002 Process 100 40 60
003 Vernish 40 10 30

the receive field is coming from total field

thx again
Go to Top of Page

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]

Go to Top of Page

taniarto
Starting Member

27 Posts

Posted - 2013-07-15 : 05:28:53
does it work on cross apply function?
Go to Top of Page

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 work

cte2 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]

Go to Top of Page

taniarto
Starting Member

27 Posts

Posted - 2013-07-15 : 22:44:16
thanks mr.khtan..
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2013-07-15 : 22:46:49
welcome


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -