如果你的表中有key :如ID的話
select tab.* ,結=(入-出) into #t from tab
update #t set 結=(select sum(t.結) from #t t where #t.ID>=t.ID)
select tab.* ,結=(入-出) into #t from tab
update #t set 結=(select sum(t.結) from #t t where #t.ID>=t.ID)
union all select in, out, 1 as result from table_name where in = 0
union all select in, out, 0 as result from table_name where in < out and in > 0
day product jin cu
2002-02-02 a180 5 3
2002-02-03 a180 0 1
2002-02-04 a180 0 0
和 表 qry day product jie
2002-02-02 a180 2
2002-02-03 a180 -1
用sql 把表 tab 和qry 变成 zon 这样:
day product jin cu jie
2002-02-02 a180 5 3 2
2002-02-03 a180 0 1 1
2002-02-04 a180 0 0 1
5-3=2 , 2-1=1, 1-0=1
jie 是由 sum(jin)-sum(cu)来的
5 2 3
0 1 2
2 2 2
5-2=3 ; 下一条 由 3+0-1=2; 再下一条 2+2-2=2;
结就是由 sum(入)-sum(出) 的.看懂了吗
,(select sum(tin)-sum(tout) from tmptest where tdate<=a.tdate) as 库存
from tmptest a
select day, product, jin, cu,
(select sum(jin)-sum(cu) from tab where day<=a.day) as jie
from tab a