2017-04-12 54 views
0

累計總和我有一個表像這樣SQL窗與分組

|week_no|value|attribute| 
------------------------- 
| 1 | 3 | a | 
| 2 | 3 | a | 
| 3 | 3 | a | 
| 1 | 4 | b | 
| 2 | 4 | b | 
| 3 | 4 | b | 

我想有一個帳戶累計值列

|week_no|value|attribute|accum_value| 
------------------------------------- 
| 1 | 3 | a |  3  | 
| 2 | 3 | a |  6  | 
| 3 | 3 | a |  9  | 
| 1 | 4 | b |  4  | 
| 2 | 4 | b |  8  | 
| 3 | 4 | b | 12  | 

我已經嘗試在做的通過使用此窗口功能儘管它似乎沒有返回正確的值

SUM(value) OVER(ORDER BY 1 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS accum_value 
+0

MySQL不支持窗口函數,所以我假設你沒有使用它。 –

回答

1

正確的勝利dow功能將使用partition by

SUM(value) OVER (PARTITION BY attribute ORDER BY week_no 
       ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 
       ) AS accum_value 
+0

這似乎並沒有工作,如果有任何我得到了相同的結果,因爲看着原始屬性,但有一些乘數形式。 – edumike