日期:2014-05-18  浏览次数:20461 次

关于数字累加的问题,请高手帮忙!
AI BI
11 KN 20
11 SU 30
23 MM 40
90 DD 50

上面表有两字段AI,BI,如果给出一个数字10,想增加一列内容为在10的基础上进行累加,如上面第三列字段内容。
不知语句如何写,请高手帮忙。TKS!

------解决方案--------------------
declare @var int
set @var = 你需要的数字
update 表名 set AI = AI + @VAR
GO
AI BI
21 KN
21 SU
33 MM
100 DD
结果应该是这样~
------解决方案--------------------
SQL code


declare @t table 
(AI        int            not null,
 BI        varchar(4)    not null
)
insert into @t
select 11,'KN' union all
select 11,'SU' union all
select 23,'MM' union all
select 90,'DD' 

declare @n int 
set @n = 10

;with t as (
    select row_number() over(order by AI ,BI ) as ID,*,@n as n from @t
)

select *,(select sum(n) from t b where b.id <= a.id) from t a