| 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_curtnxto god be the glory |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
rye07
Starting Member
13 Posts |
Posted - 2012-03-26 : 22:56:51
|
| yes i need to use cursor hereto god be the glory |
 |
|
|
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 hereto god be the glory
Then this must be home work / assignment ! KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
rye07
Starting Member
13 Posts |
Posted - 2012-03-26 : 23:19:19
|
| nope im not a student i just wanted to learn more in sqlto god be the glory |
 |
|
|
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 thiscreate 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 hereor use update cursor. Declare it for updatehttp://msdn.microsoft.com/en-us/library/ms180169.aspxupdate or_cur set ... fetch next from or_cur into @aemployeeName,@acustomerId,@aorderId,@aorderdate,@aorderamount,@arunningtotal end close or_cur deallocate or_curtnxto god be the glory
KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
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 sqlto god be the glory
But your profile there says so  KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
rye07
Starting Member
13 Posts |
Posted - 2012-03-27 : 01:17:33
|
| i cant get it??? :(to god be the glory |
 |
|
|
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] |
 |
|
|
|
|
|