读书人

数量累加有关问题

发布时间: 2013-07-04 11:45:33 作者: rapoo

数量累加问题
各位好,小弟有一问题请教,表如下:
declare @a table
(
fbqtyint,
finqtyint,
foutqtyint
)
insert into @a
select 100, 20, 30
union all
select 0, 50, 20
union all
select 0, 200, 150
union all
select 0, 100, 250
想得到结果:
fbqty finqty foutqty
----------- ----------- -----------
100 20 30
90 50 20
120 200 150
170 100 250
也就是除第一条纪录外,后面的纪录的fbqty都是前一条纪录的fbqty+finqty-foutqty
谢谢各位!
[解决办法]

引用:
Quote: 引用:


declare @a table
(
fbqty int,
finqty int,
foutqty int
)
insert into @a
select 100, 20, 30
union all
select 0, 50, 20
union all
select 0, 200, 150
union all
select 0, 100, 250
;with tb as(
select number=ROW_NUMBER()over(order by getdate())
,*,a=fbqty+finqty-foutqty from @a
)
select fbqty=isnull((select sum(a) from tb where number<a.number),fbqty),finqty,foutqty
from tb a

这是sql server 2005以上版本的语法,请问有sql server 2000的语句吗?,谢谢


加个自增列就行

读书人网 >SQL Server

热点推荐