我必须在我的应用程序中显示总计列的运行总数...所以我使用以下查询来查找运行总计...我发现两者都按照我的需要工作.在一个我使用左连接与group by和另一个我使用子查询.
现在我的问题是,当我的数据每天增加数千时,哪一个更快,如果数据将限制在1000或2000行,那么哪一个更好......而且任何其他方法比这两个更快? ??
declare @tmp table(ind int identity(1,1),col1 int)
insert into @tmp
select 2
union
select 4
union
select 7
union
select 5
union
select 8
union
select 10
SELECT t1.col1,sum( t2.col1)
FROM @tmp AS t1 LEFT JOIN @tmp t2 ON t1.ind>=t2.ind
group by t1.ind,t1.col1
select t1.col1,(select sum(col1) from @tmp as t2 where t2.ind<=t1.ind)
from @tmp as t1
Run Code Online (Sandbox Code Playgroud) 我有一个复杂的查询(包含多个连接,联合),它返回一组包含id,day,hr,amount的行.查询的输出如下所示:
id day hr amount
1 1 1 10
1 1 2 25
1 1 3 30
1 2 1 10
1 2 2 40
1 2 2 30
2 1 1 10
2 1 2 15
2 1 3 30
2 2 1 10
2 2 2 20
2 2 2 30
Run Code Online (Sandbox Code Playgroud)
我需要找到每个小时的累计总数,每天的每个小时.输出应该是这样的:
id day hr amount cumulative total
1 1 1 10 10
1 1 2 25 35
1 1 3 30 65
1 2 1 10 10 …Run Code Online (Sandbox Code Playgroud)