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

时间:2021-07-01 20:21:34

My sql table is

我的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

And I want SQL query which give below output

我想要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

Can anyone help me to write below query?

任何人都可以帮我写下面的查询?

2 个解决方案

#1


2  

You could use SUM() OVER to get cumulative sum:

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

SELECT *, SUM(Applications) OVER(PARTITION BY Year ORDER BY Week)
FROM tab

#2


1  

It looks like you want a cumulative sum:

看起来你想要一个累积总和:

select week, year,
       sum(applications) over (partition by year order by week) as cumulative_applications
from t;

#1


2  

You could use SUM() OVER to get cumulative sum:

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

SELECT *, SUM(Applications) OVER(PARTITION BY Year ORDER BY Week)
FROM tab

#2


1  

It looks like you want a cumulative sum:

看起来你想要一个累积总和:

select week, year,
       sum(applications) over (partition by year order by week) as cumulative_applications
from t;