Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将列值添加到SQL中的下一列_Sql_Sql Server - Fatal编程技术网

将列值添加到SQL中的下一列

将列值添加到SQL中的下一列,sql,sql-server,Sql,Sql Server,我的sql表是 Week Year Applications 1 2017 0 2 2017 10 3 2017 20 4 2017 50 5 2017 0 1 2018 10 2 2018 0 3 2018 40 4 2018 50 5 2018 10 我想要SQL查询,它给出下面的输出 Week

我的sql表是

Week    Year    Applications
1       2017    0
2       2017    10
3       2017    20
4       2017    50
5       2017    0
1       2018    10
2       2018    0
3       2018    40
4       2018    50
5       2018    10
我想要SQL查询,它给出下面的输出

Week    Year    Applications
1       2017    0
2       2017    10
3       2017    30
4       2017    80
5       2017    80
1       2018    10
2       2018    10
3       2018    50
4       2018    100
5       2018    110
有人能帮我写下下面的查询吗?

您可以使用
SUM()来获得累计总和:

SELECT *, SUM(Applications) OVER(PARTITION BY Year ORDER BY Week)
FROM tab
select week, year,
       sum(applications) over (partition by year order by week) as cumulative_applications
from t;

看起来您需要一个累积的总和:

SELECT *, SUM(Applications) OVER(PARTITION BY Year ORDER BY Week)
FROM tab
select week, year,
       sum(applications) over (partition by year order by week) as cumulative_applications
from t;