22,209
社区成员
发帖
与我相关
我的任务
分享
create table ta (时间 datetime,帐号 varchar(10),值 int)
insert into ta
select '2013-07-24 01:10:20',10001,101
union all select '2013-07-24 02:10:20',10001,90
union all select '2013-07-24 03:10:20',10001,100
union all select '2013-07-24 03:10:20',10002,102
create table tb(帐号 varchar(10),余额 int)
insert into tb
select 10001,10000
union all select 10002,20000
select * from ta
select * from tb
select a.*,b.余额+(select sum(值) from ta t where t.时间<=a.时间 and t.帐号=a.帐号) as 结算后余额
from ta a
left join tb b on a.帐号=b.帐号
drop table ta,tb
/*
2013-07-24 01:10:20.000 10001 101 10101
2013-07-24 02:10:20.000 10001 90 10191
2013-07-24 03:10:20.000 10001 100 10291
2013-07-24 03:10:20.000 10002 102 20102
*/