【发布时间】:2021-03-02 17:04:01
【问题描述】:
所以我的疑问在于 sql。我正在查找过去 12 个月的父帐户的总收入。 数据看起来像这样
| revenue | name | month | year |
|---|---|---|---|
| 10000 | abc | 201001 | 2010-01-12 |
| 10000 | abc | 201402 | 2014-02-14 |
| 2000 | abc | 201404 | 2014-04-12 |
| 3000 | abc | 201406 | 2014-06-30 |
| 30000 | def | 201301 | 2013-01-14 |
| 6000 | def | 201304 | 2013-04-12 |
| 9000 | def | 201407 | 2013-07-19 |
输出应该是这样的
| revenue | name | month | year | Running Sum |
|---|---|---|---|---|
| 10000 | abc | 201001 | 2010-01-12 | 10000 |
| 10000 | abc | 201402 | 2014-02-14 | 10000 |
| 2000 | abc | 201404 | 2014-04-12 | 12000 |
| 3000 | abc | 201406 | 2014-06-30 | 15000 |
| 30000 | def | 201301 | 2013-01-14 | 30000 |
| 6000 | def | 201304 | 2013-04-12 | 36000 |
| 9000 | def | 201407 | 2013-07-19 | 45000 |
我尝试过使用类似这样的窗口函数以及我需要的逻辑
select revenue, name, date, month,
sum(revenue) over (partition by name order by month rows between '12 months' preceding AND CURRENT ROW )
from table
但是上面的命令给出了语法错误
【问题讨论】:
-
很抱歉,我正在使用红移。我会更新的!
-
order by month range between current row and interval '12 months'表示“从当前行(包括当前行)和所有下一行到一年后”。使用FROM PRECEDING INTERVAL 12 MONTH AND CURRENT ROW(或者可能是 11 个月?)... -
嗨,我仍然收到语法错误。可能 redshift 不支持间隔命令。我也在他们的文档中看到了这一点
标签: sql amazon-redshift window-functions