【发布时间】:2020-08-15 10:29:34
【问题描述】:
我有一张这样的桌子:
month region value
4/1/20 eu-west-2 110
3/1/20 eu-west-2 30
2/1/20 eu-west-2 13
2/1/20 us-west-2 2
1/1/20 us-west-2 242
4/1/20 us-west-2 240
我想找到每个区域每个月的“值”列的累积总和。例如:使用上面的示例答案应该是:
month region cumulative_sum
4/1/20 eu-west-2 153
3/1/20 eu-west-2 43
2/1/20 eu-west-2 13
4/1/20 us-west-2 484
2/1/20 us-west-2 244
1/1/20 us-west-2 242
当我只找到每个月的累积总和时,我能够成功编写查询,但是当我向其中添加区域时它不起作用:
select
month, sum(value) over (order by month rows unbounded preceding) as cumulative_sum
from table
但是当我这样做时:
select
month, region, sum(value) over (order by month,region rows unbounded preceding) as cumulative_sum
from table
它给出了错误的结果。
请帮忙。
【问题讨论】:
标签: sql select amazon-redshift window-functions cumulative-sum