select 编号,
日期,
贷方金额,
借方金额,
余额= ( select sum(isnull(贷方金额,0))-sum(isnull(借方金额,0))
from tablename
where 编号<=a.编号
)
from tablename a
order by 编号
解决方案 »
- sql 2009-11-10
- [急...]多表查询,当一个表没有符合条件的记录时,是不是总的查询结果也为空?
- 将 excel导到 sql server2000中的问题,皱建大哥帮忙呀?
- 用存储过程往sql里写数据时,为什么会自动把字符型数据前面的零给去掉
- 多表联接语法问题
- Mssql安装完成后,没有mssql management Studio,该怎么解决!!
- 求命!!!极难的SQL
- 怎么查找数据库中的某个字段
- ####请问sql7.0+winnt4所需的机器配置####
- 又是一个sql语句!
- 有沒有辦法用sql語句斷開當前的連接?
- 各位大虾介绍本好点的SQL server 2000的书给小弟好吗?
首先为数据添加一个标识字段,既然修改表结构不行,那就用余额这个字段吧,这总可以吧Update 余额 = Null --用作标识位
Update 余额 = Isnull(贷方金额 - 借方金额,0) where 编号日期是最小的一个
while Exists(select IsCom from TableA where 余额 is null)
Update 余额 = Isnull(b.上一条记录余额+a.本条记录贷方金额-a.本条记录借方金额,0)
from TableA a, (select 编号,max(日期) from TableA where 余额 is not null group by 编号) b
(select 编号,min(日期) from TableA where 余额 is null group by 编号) c,
(select * from TableA) d
where b.编号=d.编号 and b.日期=d.日期 and a.编号=c.编号 and a.日期=c.日期
and a.编号 = d.编号最后: select * from TableA order by 编号,日期即可或者你在前台也可以
sum(isnull(b.贷方金额,0))-sum(isnull(b.借方金额,0))+sum(a.贷方金额)-sum(b.借方金额)as 余额 from table1 a,table b where a.编号>b.编号