现在用access做报表:客户台帐表 客户 销售金额 到账金额 余额
01 10000 5000 5000
01 2000 3000
01 4000 7000
其中 销售金额和到账金额分别从销售表和到账表引入,现在想请教 “余额 ” 的值应该如何做呢?!!
即 余额=余额+销售金额-到账金额 ,有能直接实现此功能的函数么?
用sql或者vba怎么实现呢?
01 10000 5000 5000
01 2000 3000
01 4000 7000
其中 销售金额和到账金额分别从销售表和到账表引入,现在想请教 “余额 ” 的值应该如何做呢?!!
即 余额=余额+销售金额-到账金额 ,有能直接实现此功能的函数么?
用sql或者vba怎么实现呢?
update 客户台帐表 set 余额=余额+销售金额-到账金额
---如果在几张表还需要关联
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
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 行受影响)