2015-04-17 56 views
4

我目前正在研究一個示例腳本,它允許我計算前兩行和當前行的總和。但是,我想將數字「2」作爲變量。我試過聲明一個變量,或者直接在查詢中強制轉換,但總會彈出一個語法錯誤。有沒有可能的解決方案?使用窗口函數計算移動總和/平均值時的動態行範圍(SQL Server)

DECLARE @myTable TABLE (myValue INT) 

INSERT INTO @myTable (myValue) VALUES (5) 
INSERT INTO @myTable (myValue) VALUES (6) 
INSERT INTO @myTable (myValue) VALUES (7) 
INSERT INTO @myTable (myValue) VALUES (8) 
INSERT INTO @myTable (myValue) VALUES (9) 
INSERT INTO @myTable (myValue) VALUES (10) 

SELECT 
    SUM(myValue) OVER (ORDER BY myValue 
         ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) 
FROM @myTable 
+0

之間進行切換,然後執行它? – gotqn

+0

是的,我可以嘗試 - 目前它似乎是唯一的出路。但是想要檢查是否有其他「更清潔」的解決方案!非常感謝! – test

回答

4
DECLARE @test VARCHAR = 1 
DECLARE @sqlCommand VARCHAR(1000) 
DECLARE @myTable TABLE (myValue INT) 

INSERT INTO @myTable (myValue) VALUES (5) 
INSERT INTO @myTable (myValue) VALUES (6) 
INSERT INTO @myTable (myValue) VALUES (7) 
INSERT INTO @myTable (myValue) VALUES (8) 
INSERT INTO @myTable (myValue) VALUES (9) 
INSERT INTO @myTable (myValue) VALUES (10) 

SET @sqlCommand = 'SELECT SUM(myValue) OVER (ORDER BY myValue ROWS BETWEEN ' + @test + ' PRECEDING AND CURRENT ROW) 
        FROM #temp' 

EXEC (@sqlCommand) 
4

你可以嘗試這樣的事情不使用動態SQL。

DECLARE @myTable TABLE (myValue INT) 

INSERT INTO @myTable (myValue) VALUES (5) 
INSERT INTO @myTable (myValue) VALUES (6) 
INSERT INTO @myTable (myValue) VALUES (7) 
INSERT INTO @myTable (myValue) VALUES (8) 
INSERT INTO @myTable (myValue) VALUES (9) 
INSERT INTO @myTable (myValue) VALUES (10) 


DECLARE @prev_records INT = 2 

;WITH CTE as 
(
SELECT ROW_NUMBER() OVER(ORDER BY myValue) rn,myValue FROM @myTable 
) 
SELECT (SELECT SUM(myValue) FROM CTE t2 WHERE t2.rn BETWEEN (t1.rn - @prev_records) AND t1.rn) 
FROM CTE t1 

SUM(myValue) OVER()然而它不允許你通過使用一個變量前面N行的最佳選擇。

1

如果範圍變量的可能分散不是很高,可以用簡單的CASE聲明你可以建立一個動態的`T-SQL`語句計算

DECLARE @myTable TABLE (myValue INT) 

-- let's say it could be between 1 and 10 
DECLARE @range int = 3; 

INSERT INTO @myTable (myValue) VALUES (5) 
INSERT INTO @myTable (myValue) VALUES (6) 
INSERT INTO @myTable (myValue) VALUES (7) 
INSERT INTO @myTable (myValue) VALUES (8) 
INSERT INTO @myTable (myValue) VALUES (9) 
INSERT INTO @myTable (myValue) VALUES (10) 

SELECT 
    CASE @range 
     WHEN 1 THEN SUM(myValue) OVER (ORDER BY myValue ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) 
     WHEN 2 THEN SUM(myValue) OVER (ORDER BY myValue ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) 
     WHEN 3 THEN SUM(myValue) OVER (ORDER BY myValue ROWS BETWEEN 3 PRECEDING AND CURRENT ROW) 
     WHEN 4 THEN SUM(myValue) OVER (ORDER BY myValue ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) 
     WHEN 5 THEN SUM(myValue) OVER (ORDER BY myValue ROWS BETWEEN 5 PRECEDING AND CURRENT ROW) 
     WHEN 6 THEN SUM(myValue) OVER (ORDER BY myValue ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) 
     WHEN 7 THEN SUM(myValue) OVER (ORDER BY myValue ROWS BETWEEN 7 PRECEDING AND CURRENT ROW) 
     WHEN 8 THEN SUM(myValue) OVER (ORDER BY myValue ROWS BETWEEN 8 PRECEDING AND CURRENT ROW) 
     WHEN 9 THEN SUM(myValue) OVER (ORDER BY myValue ROWS BETWEEN 9 PRECEDING AND CURRENT ROW) 
     WHEN 10 THEN SUM(myValue) OVER (ORDER BY myValue ROWS BETWEEN 10 PRECEDING AND CURRENT ROW) 
    END 
FROM @myTable