如果你的表中有key :如ID的話
select tab.* ,結=(入-出) into #t from tab 
update #t set 結=(select sum(t.結) from #t t where #t.ID>=t.ID)

解决方案 »

  1.   

    select in, out, (in - out) as result from table_name where in >= out and in > 0
    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
      

  2.   

    不用了,让大家看这个难度小一点的.表  tab
       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)来的
      

  3.   

    to tj_dns     入   出   结
                  5    2    3
                  0    1    2
                  2    2    2
    5-2=3 ;   下一条  由  3+0-1=2;  再下一条  2+2-2=2;
    结就是由 sum(入)-sum(出) 的.看懂了吗
      

  4.   

    select a.tin 入,a.tout 出,a.tdate 时间
    ,(select sum(tin)-sum(tout) from tmptest where tdate<=a.tdate) as 库存
    from tmptest a
      

  5.   

    如果是这样,那楼上的思路就是正确的.更准确的写法是:
    select day, product, jin, cu, 
    (select sum(jin)-sum(cu) from tab where day<=a.day) as jie 
    from tab a