用先前的行值更新行 [英] update row with previous rows value
问题描述
我有变量Table
i have variable Table
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)
我需要总列为当前行+上一行的总和(value1 + value2)
所以结果会像这样
Value1个值总计
---------------------
1 1 2
2 2 6
3 3 12
4 4 20
5 5 30
所以我怎么计算总数
我不想使用循环,因为在实际示例中,我大约有2000行或更多行,因此我使用任何循环语句都将花费太多时间
我需要保持良好的性能
[代码]阻止更新了
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
推荐答案
尝试以下操作:
在SQL Server中计算简单运行总计 [ ^ ]
希望这会有所帮助,
巴勃罗.
Try this:
Calculating simple running totals in SQL Server[^]
Hope this helps,
Pablo.
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)
这篇关于用先前的行值更新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!