读书人

怎么得到余额?怎么得到余额?怎么得到

发布时间: 2013-12-20 00:23:10 作者: rapoo

如何得到余额?如何得到余额?如何得到余额?
如何得到余额?
会计凭证表(KJ)
id jfmoney(借方金额) dfmoney(贷方金额)
1 100 200
2 120 120
3 150 150
... .... ...
如何得到余额
余额=上一行的余额-货方+借方。借就加,货就减。贷和借不会出现在同一行。


[解决办法]

引用:
如何得到余额?
会计凭证表(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
/*
idjfmoneydfmoney余额
1100 0 100
20 120 -20
3150 0 130
40 300 -170
*/

[解决办法]
引用:
对了 你上面的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
/*
idjfmoneydfmoney余额
1100 0 100
20 120 -20
3150 0 130
40 300 -170
*/


高手 能解释 一下吗 没看懂啊
[解决办法]
引用:
对了 你上面的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
/*
idjfmoneydfmoney余额
1100 0 100
20 120 -20
3150 0 130
40 300 -170
*/

那个 sum(余额)是吧所有余额相加 那 个 id>=id 是 什么意思啊
[解决办法]
引用:
Quote: 引用:

对了 你上面的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
/*
idjfmoneydfmoney余额
1100 0 100
20 120 -20
3150 0 130
40 300 -170
*/

那个 sum(余额)是吧所有余额相加 那 个 id>=id 是 什么意思啊


哦,这个里面是一个子查询,就是查找 id比当前t1.id小的,所有的记录,然后求和余额。

举例就是,id为1就是求他自己的余额,id为2就求1和2的余额的sum,以此类推哈

读书人网 >SQL Server

热点推荐