探索SQL问题,高手请进!(50分)

  • 主题发起人 主题发起人 qdgm988
  • 开始时间 开始时间
谢谢QuickSilver,速度确有提高,不过对于大数据量来说,速度仍无法让人满意
不知还有无其他的思路和方法?

再次对QuickSilver表示感谢!
 
QuickSilver!这个方法不错
 
做了个存储过程来运算,自己试试吧,想要再快的话,我就设法了,呵

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
 
再次感谢QuickSilver,谢谢!
 
接受答案了.
 
一个存储过程 + 游标
(因为这是一个明细,所以可能要每次查询的时候排序的顺序是要求相同的,如过你的
日期字段够精确的话,可以用之来排序,否则的话,还得加个辅助字段,在计算之前先
得排序好。) 一个简单例子(修改后可以运行):

CREATE PROCEDURE dbo.MXZ
AS
declare @YE float
declare @Date Date
select DATE,sum(收入) as 收入,sum(支出) as 支出,sum(收入-支出) as 余额 into #AAA from table group by DATE
declare EdtYe scroll cursor for select DATE,(收入-支出) as 余额 from #AAA
open EdtYe
Fetch first from EdtYe into @Date,@YE
while @@Fetch_status=0
begin
update #AAA set 余额=余额+@YE where current of EdtYe
Fetch next from EdtYe into @Date,@YE
end
close EdtYe
deallocate EdtYe
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部