0

我的记录在一个具有三列的临时表中:

  1. 第 1 列:ID(大整数)
  2. Column2:CreationDateTime(日期时间)
  3. Column3:体积(浮点数)

记录根据 CreationDateTime 进行排序。我需要从表中选择卷总和等于 THRESHOLD1 的记录,然后对于 Threshold2 相同。

一种方法是向表中添加一个新列,该表具有先前记录的 Volume 总和。例如 :

ID - CreationDateTime - 卷 - 总和

1 - 20/07/2012 - 10 - 10

2 - 21/07/2012 - 12 - 22

3 - 22/07/2012 - 7 - 29

然后 Select * from temp where Sum >= Threshold 但是计算总和并不是最快的方法。

我想知道是否有人可以提出更好的方法来执行上述操作。

我正在使用 SQL Server 2008,如果需要,我也可以使用 CLR。

4

2 回答 2

1

试试这个解决方案:

您可以通过自我加入表格和分组来找到运行总数

with cte as(
select T2.ID, T2.CreationDateTime,SUM(T1.Volume) [SUM]
from test_table T1 join  test_table T2
on T1.id<=T2.id
group by T2.id, T2.CreationDateTime)
select * from cte where [SUM]>= Threshold
于 2012-07-25T10:44:47.967 回答
0

这是一种使用递归 CTE 的方法,它可能是最快的:

select @i=min(ID) from @temp

;with a as 
( 
    select ID, Volume, Volume as RunningTotal 
    from @temp
    where ID=@i 

    union all 
    select b.ID, b.Volume, b.Volume + a.RunningTotal as RunningTotal 
    from @temp b 
        inner join a 
            on b.ID=a.ID+1 

) 
select * from a 

与运行总计相关的一些链接:

http://www.sqlusa.com/bestpractices/runningtotal/

http://www.databasejournal.com/features/mssql/article.php/3112381/SQL-Server-Calculating-Running-Totals-Subtotals-and-Grand-Total-Without-a-Cursor.htm

http://www.mssqltips.com/sqlservertip/1686/calculate-running-totals-using-sql-server-cross-joins/

http://social.msdn.microsoft.com/Forums/eu/transactsql/thread/1b4d87cb-ec77-4455-af48-bf7dae50ab87

使用函数计算的列:

create function dbo.fn_VolumeRunningTotal 
{ 
    @dt datetime 
} 
returns int 
as  
begin 
    declare @total int 
    select @total = sum(volume) 
    from dbo.MyVolumeTable 
    where CreationDateTime <= @dt 

    return @total 
end 

计算列公式:

dbo.fn_VolumeRunningTotal(CreationDateTime) 

选择语句:

select * from dbo.MyVolumnTable where RunningTotal <= @Threshold1 
于 2012-07-25T10:29:01.390 回答