SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Other SQL Server Topics (2005)
 Running Total
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

taniarto
Starting Member

Indonesia
27 Posts

Posted - 06/30/2013 :  23:22:52  Show Profile  Reply with Quote
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)

Singapore
17650 Posts

Posted - 06/30/2013 :  23:33:32  Show Profile  Reply with Quote

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
Time is always against us

Go to Top of Page

taniarto
Starting Member

Indonesia
27 Posts

Posted - 07/01/2013 :  05:34:26  Show Profile  Reply with Quote
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

India
52323 Posts

Posted - 07/01/2013 :  06:07:26  Show Profile  Reply with Quote
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)

Singapore
17650 Posts

Posted - 07/01/2013 :  07:39:56  Show Profile  Reply with Quote
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
Time is always against us


Edited by - khtan on 07/01/2013 07:40:24
Go to Top of Page

taniarto
Starting Member

Indonesia
27 Posts

Posted - 07/05/2013 :  02:44:39  Show Profile  Reply with Quote
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
Time is always against us



Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17650 Posts

Posted - 07/05/2013 :  02:47:24  Show Profile  Reply with Quote
quote:
If the result is running decrease what is the formula ?

Example ?


KH
Time is always against us

Go to Top of Page

taniarto
Starting Member

Indonesia
27 Posts

Posted - 07/05/2013 :  03:58:50  Show Profile  Reply with Quote
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
Time is always against us



Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17650 Posts

Posted - 07/05/2013 :  04:45:37  Show Profile  Reply with Quote
how does the original data looks like ?

Where is the "Start" value comes from ?


KH
Time is always against us

Go to Top of Page

taniarto
Starting Member

Indonesia
27 Posts

Posted - 07/05/2013 :  05:25:18  Show Profile  Reply with Quote
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
Time is always against us



Go to Top of Page

taniarto
Starting Member

Indonesia
27 Posts

Posted - 07/05/2013 :  22:31:33  Show Profile  Reply with Quote
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
Time is always against us





Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17650 Posts

Posted - 07/05/2013 :  23:34:38  Show Profile  Reply with Quote
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
Time is always against us


Edited by - khtan on 07/05/2013 23:36:33
Go to Top of Page

taniarto
Starting Member

Indonesia
27 Posts

Posted - 07/15/2013 :  04:19:08  Show Profile  Reply with Quote
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)

Singapore
17650 Posts

Posted - 07/15/2013 :  04:36:28  Show Profile  Reply with Quote

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



KH
Time is always against us

Go to Top of Page

taniarto
Starting Member

Indonesia
27 Posts

Posted - 07/15/2013 :  05:28:53  Show Profile  Reply with Quote
does it work on cross apply function?
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17650 Posts

Posted - 07/15/2013 :  05:50:42  Show Profile  Reply with Quote
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
Time is always against us

Go to Top of Page

taniarto
Starting Member

Indonesia
27 Posts

Posted - 07/15/2013 :  22:44:16  Show Profile  Reply with Quote
thanks mr.khtan..
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17650 Posts

Posted - 07/15/2013 :  22:46:49  Show Profile  Reply with Quote
welcome


KH
Time is always against us

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000