现在用access做报表:客户台帐表         客户      销售金额        到账金额          余额 
        01      10000        5000        5000 
    01                    2000        3000 
    01      4000                      7000 
其中 销售金额和到账金额分别从销售表和到账表引入,现在想请教  “余额 ” 的值应该如何做呢?!! 
即  余额=余额+销售金额-到账金额 ,有能直接实现此功能的函数么? 
用sql或者vba怎么实现呢?

解决方案 »

  1.   

    update 客户台帐表 set 余额=余额+销售金额-到账金额
      

  2.   

    --如果已经在一张表了 就
    update 客户台帐表 set 余额=余额+销售金额-到账金额
    ---如果在几张表还需要关联
      

  3.   

    create table tb(id int , 客户 varchar(10), 销售金额 int, 到账金额 int, 余额 int)
    insert into tb values(1,'01', 10000, 5000, 0)
    insert into tb values(2,'01', 0 , 2000 , 0)
    insert into tb values(3,'01', 4000 , 0,0)
    go--search
    select 客户,      
           销售金额,
           到账金额,
           余额 = (select sum(销售金额) - sum(到账金额) from tb where id <= t.id)
    from tb t
    /*
    客户         销售金额        到账金额        余额          
    ---------- ----------- ----------- ----------- 
    01         10000       5000        5000
    01         0           2000        3000
    01         4000        0           7000(所影响的行数为 3 行)
    */--update
    update tb 
           set 余额 = (select sum(销售金额) - sum(到账金额) from tb where id <= t.id)
    from tb t
    select * from tb
    /*
    id          客户         销售金额        到账金额        余额          
    ----------- ---------- ----------- ----------- ----------- 
    1           01         10000       5000        5000
    2           01         0           2000        3000
    3           01         4000        0           7000(所影响的行数为 3 行)
    */
    drop table tb
      

  4.   

    晕死,这2天这个问题,我这样做,老是不行。不知道哪里有问题--> 测试数据: @tb
    declare @tb table (客户 varchar(2),销售金额 int,到账金额 int,余额 int)
    insert into @tb
    select '01',10000,5000,0 union all
    select '01',0,2000,0 union all
    select '01',4000,0,nulldeclare @s int,@n int
    set @s=0update @tb 
    set @s=销售金额-到账金额,余额=@s+isnull(@n,0),@n=余额  select * from @tb
    客户   销售金额        到账金额        余额
    ---- ----------- ----------- -----------
    01   10000       5000        5000
    01   0           2000        -2000
    01   4000        0           4000(3 行受影响)