做了个存储过程来运算,自己试试吧,想要再快的话,我就设法了,呵
CREATE PROCEDURE [temp] AS
begin
declare @余额 real
declare @id int
select identity(int,1,1) as id,
convert(varchar(10),日期,20) as 日期,
sum(收入) as 收入,
sum(支出) as 支出,
sum(收入-支出) as 余额
into #temp
from table1
group by convert(varchar(10),日期,20)
declare loop_cursor cursor for select id from #temp
open loop_cursor
fetch next from loop_cursor into @id
set @余额=0
while @@FETCH_STATUS = 0
begin
update #temp set 余额 = @余额+收入-支出 where current of loop_cursor
update #temp set @余额=余额 where current of loop_cursor
fetch next from loop_cursor into @id
end
close loop_cursor
deallocate loop_cursor
select * from #temp
end
GO