如何得到余额?
会计凭证表(KJ)
id jfmoney(借方金额) dfmoney(贷方金额)
1 100 200
2 120 120
3 150 150
... .... ...
如何得到余额
余额=上一行的余额-货方+借方。借就加,货就减。贷和借不会出现在同一行。
------解决方案--------------------
需要怎样的结果,能贴处理吗
------解决方案--------------------
应该是怎样的,能不能把你想要的结果,重新贴出来
------解决方案--------------------
对了 你上面的3 150 0 -20-0+150=120 ,余额应该是130吧,
是这样吗:
drop table tb
create table tb(id int,jfmoney int,dfmoney int)
insert into tb
select 1 ,100 , 0
union all select 2 , 0 , 120
union all select 3 ,150 ,0
union all select 4 , 0,300
;with t
as
(
select *,
jfmoney - dfmoney as yue
from tb t1
)
select id,jfmoney,dfmoney,
(select isnull(sum(yue),0)
from t t2
where t1.id >= t2.id ) 余额
from t t1
/*
id jfmoney dfmoney 余额
1 100 0 100
2 0 120 -20
3 150 0 130
4 0 300 -170
*/
------解决方案--------------------