首页 新闻 会员 周边

SQL Server 的数据累加问题?

0
悬赏园豆:30 [已解决问题] 解决于 2008-10-07 22:52

有表: id  value

    1  10

    2  20  

    3  50

    4  100

请问如何直接通过SQL语句得到查询结果:

  id  value

  1    10  

  2    30  // 30 = 20 + 10

  3    80  // 80 = 50 + 30

  4    180  // 180 = 100 + 80

如果使用游标如何实现这个功能?如果不使用游标又如何实现这个功能?要实现这个功能有有可能吗?

沈啣结的主页 沈啣结 | 初学一级 | 园豆:0
提问于:2008-10-07 08:49
< >
分享
最佳答案
2

Code
declare @t table(id int ,num int)
insert @t
select 1,10 union all
select 2,20 union all
select 3,50 union all
select 4,100
select
a.id,(
select sum(num) from @t b where b.id<=a.id)
from @t a

 

 

 

roboth | 初学一级 |园豆:28 | 2008-10-07 13:52
其他回答(1)
0

Robot-H

请问怎么直接将结果更新至 @t 中呢? 有不用游标的方法吗?要用中间表吗?

| 园豆:770 (小虾三级) | 2008-10-07 17:52
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册