我有下表(表1):
+----+-----+
|Type|Value|
+----+-----+
|P |4 |
+----+-----+
|P |20 |
+----+-----+
|S |0 |
+----+-----+
|S |0 |
+----+-----+
|S |0 |
+----+-----+
我正在运行以下查询:
update Table1 set value = ( select sum(value) FROM Table1 ) where Type = 'S'
对于所有值为'S'
的字段,结果为24我需要考虑运行时更新的值。 结果我需要:
+----+-----+
|Type|Value|
+----+-----+
|P |4 |
+----+-----+
|P |20 |
+----+-----+
|S |24 |
+----+-----+
|S |48 |
+----+-----+
|S |96 |
+----+-----+
如何在简单查询中完成此操作?
谢谢,
答案 0 :(得分:1)
DECLARE @ID int
DECLARE cur CURSOR local select ID from table1 where Type = 'S'
FETCH cur into @ID
WHILE @@FETCH_STATUS=0
BEGIN
update Table1 set value = ( select sum(value) FROM Table1 ) where ID=@ID
FETCH cur into @ID
END
Close cur
DEALLOCATE cur