特定年份和月份的每条记录的累计值

时间:2017-09-22 08:49:06

标签: sql-server

我的查询有问题。我有一张桌子。表中的值是不同的,我在每行中呈现相同的值(10)以简化和更好地理解问题

| ID | UniqueProcutId | Year | Month | Value |
-----|----------------|------|-------|-------|
| 1  | wwee124        | 2015 | 1     | 10    |
| 2  | wwee124        | 2015 | 2     | 10    |
| 3  | wwee124        | 2015 | 3     | 10    |
| 4  | wwee124        | 2015 | 4     | 10    |
| 5  | wwee124        | 2015 | 5     | 10    |
| 6  | wwee124        | 2015 | 6     | 10    |
| 7  | wwee124        | 2015 | 7     | 10    |
| 8  | wwee124        | 2015 | 8     | 10    |
| 9  | wwee124        | 2015 | 9     | 10    |
| 10 | wwee124        | 2015 | 10    | 10    |
| 11 | wwee124        | 2015 | 11    | 10    |
| 12 | wwee124        | 2015 | 12    | 10    |
| 13 | wwee124        | 2016 | 1     | 10    |
| 14 | wwee124        | 2016 | 2     | 10    |
| 15 | wwee124        | 2016 | 3     | 10    |

我想要实现的是查询将返回一年中每个月的累积值。我的意思是:

SELECT ID, PRODUCTID, YEAR, MONTH,
SUM(VALUE) OVER(?????)

我无法处理它:( 查询应返回:

| ID | UniqueProcutId | Year | Month | Value |
-----|----------------|------|-------|-------|
| 1  | wwee124        | 2015 | 1     | 10    |
| 2  | wwee124        | 2015 | 2     | 20    |
| 3  | wwee124        | 2015 | 3     | 30    |
| 4  | wwee124        | 2015 | 4     | 40    |
| 5  | wwee124        | 2015 | 5     | 50    |
| 6  | wwee124        | 2015 | 6     | 60    |
| 7  | wwee124        | 2015 | 7     | 70    |
| 8  | wwee124        | 2015 | 8     | 80    |
| 9  | wwee124        | 2015 | 9     | 90    |
| 10 | wwee124        | 2015 | 10    | 100   |
| 11 | wwee124        | 2015 | 11    | 110   |
| 12 | wwee124        | 2015 | 12    | 120   |
| 13 | wwee124        | 2016 | 1     | 10    |
| 14 | wwee124        | 2016 | 2     | 20    |
| 15 | wwee124        | 2016 | 3     | 30    |

1 个答案:

答案 0 :(得分:1)

你很亲密。试试这个:

view