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
 General SQL Server Forums
 New to SQL Server Programming
 running total

Author  Topic 

rye07
Starting Member

13 Posts

Posted - 2012-03-26 : 22:51:31
can any one help me how can i get the running total of this

create table #aorderlist (employeeName varchar(50),customerId nchar(5),orderID int,OrderDate Date,OrderAmount int,RunningTotal int)


insert into #aorderlist (employeeName,customerId,orderId,OrderDate,OrderAmount)
select dbo.[Employees].LastName +' '+ dbo.[Employees].FirstName as employeeName,orders.customerid,orders.orderid,orders.orderdate,orders.Freight orderamount
from Orders,products,Employees
where OrderDate ='1996-07-04'
group by dbo.[Employees].LastName +' '+ dbo.[Employees].FirstName,customerId,orderId,OrderDate,orders.Freight


declare @aemployeeName varchar(50),
@acustomerId nchar(5),
@aorderId int,
@aorderdate date,
@aorderamount int,
@arunningtotal int

set @arunningtotal=0
declare or_cur cursor for
select employeeName,customerId,orderid,OrderDate,OrderAmount,RunningTotal from #aorders
open or_cur
fetch next from or_cur into @aemployeeName,@acustomerId,@aorderId,@aorderdate,@aorderamount,@arunningtotal
while @@FETCH_STATUS=0
begin
set @arunningtotal=@arunningtotal+@aorderamount
insert #aorderlist values (@aemployeeName,@acustomerId,@aorderId,@aorderdate,@aorderamount,@arunningtotal)


fetch next from or_cur into @aemployeeName,@acustomerId,@aorderId,@aorderdate,@aorderamount,@arunningtotal
end

close or_cur
deallocate or_cur



tnx

to god be the glory

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-26 : 22:55:04
what's wrong with the solution here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=172932 ?

Is it because it does not make use of cursor ?


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

Go to Top of Page

rye07
Starting Member

13 Posts

Posted - 2012-03-26 : 22:56:51
yes i need to use cursor here

to god be the glory
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-26 : 23:15:07
quote:
Originally posted by rye07

yes i need to use cursor here

to god be the glory



Then this must be home work / assignment !


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

Go to Top of Page

rye07
Starting Member

13 Posts

Posted - 2012-03-26 : 23:19:19
nope im not a student i just wanted to learn more in sql

to god be the glory
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-26 : 23:41:07
anyway, since you have attempted something,

quote:
Originally posted by rye07

can any one help me how can i get the running total of this

create table #aorderlist (employeeName varchar(50),customerId nchar(5),orderID int,OrderDate Date,OrderAmount int,RunningTotal int)


insert into #aorderlist (employeeName,customerId,orderId,OrderDate,OrderAmount)
select dbo.[Employees].LastName +' '+ dbo.[Employees].FirstName as employeeName,orders.customerid,orders.orderid,orders.orderdate,orders.Freight orderamount
from Orders,products,Employees
where OrderDate ='1996-07-04'
group by dbo.[Employees].LastName +' '+ dbo.[Employees].FirstName,customerId,orderId,OrderDate,orders.Freight


declare @aemployeeName varchar(50),
@acustomerId nchar(5),
@aorderId int,
@aorderdate date,
@aorderamount int,
@arunningtotal int

set @arunningtotal=0
declare or_cur cursor for
select employeeName,customerId,orderid,OrderDate,OrderAmount,RunningTotal from #aorders
open or_cur
fetch next from or_cur into @aemployeeName,@acustomerId,@aorderId,@aorderdate,@aorderamount,@arunningtotal
while @@FETCH_STATUS=0
begin
set @arunningtotal=@arunningtotal+@aorderamount
insert #aorderlist values (@aemployeeName,@acustomerId,@aorderId,@aorderdate,@aorderamount,@arunningtotal)

you already have the running total in @arunningtotal, you only need to update back to your #aorderlist table. Just use a UPDATE command to do it over here

or use update cursor. Declare it for update
http://msdn.microsoft.com/en-us/library/ms180169.aspx
update or_cur set ...



fetch next from or_cur into @aemployeeName,@acustomerId,@aorderId,@aorderdate,@aorderamount,@arunningtotal
end

close or_cur
deallocate or_cur



tnx

to god be the glory




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

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-26 : 23:41:14
quote:
Originally posted by rye07

nope im not a student i just wanted to learn more in sql

to god be the glory



But your profile there says so



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

Go to Top of Page

rye07
Starting Member

13 Posts

Posted - 2012-03-27 : 01:17:33
i cant get it??? :(

to god be the glory
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-27 : 02:29:31
quote:
Originally posted by rye07

i cant get it??? :(

to god be the glory



Can't get what ?


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

Go to Top of Page
   

- Advertisement -