T-SQL计算移动平均线

时间:2014-10-28 20:58:30

标签: sql tsql sql-server-2008-r2 window-functions moving-average

我正在使用SQL Server 2008 R2,尝试计算移动平均线。对于我视图中的每条记录,我想收集250条先前记录的值,然后计算此选择的平均值。

我的观点栏如下:

TransactionID | TimeStamp           | Value | MovAvg
----------------------------------------------------
            1 | 01.09.2014 10:00:12 |     5 |    
            2 | 01.09.2014 10:05:34 |     3 | 
...
          300 | 03.09.2014 09:00:23 |     4 | 

TransactionID是独一无二的。对于每个TransactionID,我想计算列值的平均值,超过之前的250条记录。因此,对于TransactionID 300,收集前250行的所有值(视图按TransactionID降序排序),然后在MovAvg列中写入这些值的平均值的结果。我希望收集一系列记录中的数据。

2 个答案:

答案 0 :(得分:21)

与更高版本相比,SQL 2008中的窗口函数相当有限,如果我记得正确,您只能进行分区,并且不能使用任何行/范围框架限制,但我认为这可能是您想要的:

;WITH cte (rn, transactionid, value) AS (
    SELECT 
       rn = ROW_NUMBER() OVER (ORDER BY transactionid),
       transactionid,
       value
    FROM your_table
)

SELECT 
    transactionid, 
    value, 
    movagv = (
        SELECT AVG(value) 
        FROM cte AS inner_ref
        -- average is calculated for 250 previous to current row inclusive
        -- I might have set the limit one row to large, maybe it should be 249
        WHERE inner_ref.rn BETWEEN outer_ref.rn-250 AND outer_ref.rn
        ) 
FROM cte AS outer_ref

请注意,它会将相关的子查询应用于每一行,但性能可能不会很好。

对于更高版本,您可以使用窗口框架功能并执行以下操作:

SELECT 
    transactionid, 
    value,
    -- avg over the 250 rows counting from the previous row
    AVG(value) OVER (ORDER BY transactionid  
                     ROWS BETWEEN 251 PRECEDING AND 1 PRECEDING),
    -- or 250 rows counting from current
    AVG(value) OVER (ORDER BY transactionid  
                     ROWS BETWEEN 250 PRECEDING AND CURRENT ROW)
FROM your_table

答案 1 :(得分:5)

使用Common Table Expression (CTE)为每个事务包含rownum,然后在行号上加入CTE,以便您可以获取之前的值来计算平均值。

CREATE TABLE MyTable (TransactionId INT, Value INT)

;with Data as
(
  SELECT TransactionId, 
         Value, 
         ROW_NUMBER() OVER (ORDER BY TransactionId ASC) as rownum
  FROM MyTable
)
SELECT d.TransactionId , Avg(h.Value) as MovingAverage
FROM Data d
JOIN Data h on h.rownum between d.rownum-250 and d.rownum-1
GROUP BY d.TransactionId