将列值添加到SQL中的下一列
我的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
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;