Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have variable Table
SQL
declare @Table Table
(
Value1 float,
Value2 float,
Total float
)
Insert into @Table values (1,1)
Insert into @Table values (2,2)
Insert into @Table values (3,3)
Insert into @Table values (4,4)
Insert into @Table values (5,5)


i need the total column to be the sum(value1+value2) from the current row +Previous Rows


so the result 'll be like that

Value1 Values Total
---------------------
1 1 2
2 2 6
3 3 12
4 4 20
5 5 30

so how i can calculate the total
i don't want to use loop because in the real example i have about 2000 rows or more so of i use any loop statement it 'll took too much time
i need to keep the performance good

[code] block updated
Posted
Updated 10-Jul-12 18:09pm
v2

Try this:
Calculating simple running totals in SQL Server[^]

Hope this helps,
Pablo.
 
Share this answer
 
SQL
create trigger trig_product
on table
after insert
as 
declare @input varchar(25)
set @input = (select distinct    max(Total)  from  table)
update  table
	set  Total = @input 
	from  table where ID=(select max(ID) from Table)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900