我有这样的桌子
+----+--------+------+------+
| id | state | num | pop |
+----+--------+------+------+
| 1 | ny | 1 | 100 |
| 1 | ny | 2 | 200 |
| 1 | ny | 3 | 600 |
| 1 | ny | 6 | 400 |
| 1 | ny | 7 | 300 |
| 1 | ny | 14 | 1000 |
| 2 | nj | 3 | 250 |
+----+--------+------+------+
我想要输出如下
+---+----+----+------+------+
| 1 | ny | 1 | 100 | 900 |
| 1 | ny | 2 | 200 | 900 |
| 1 | ny | 3 | 600 | 900 |
| 1 | ny | 6 | 400 | 700 |
| 1 | ny | 7 | 300 | 700 |
| 1 | ny | 14 | 1000 | 1000 |
| 2 | nj | 3 | 250 | 250 |
+---+----+----+------+------+
因此,如果num列中有一个seq,那么我们必须添加pop列。因此,前3列num列依次有1,2,3,因此我们添加了弹出列100 + 200 + 600并显示为新列。
我尝试了下面的代码,但没有收到想要的输出
select id, state,num, pop,
sum(pop) over (partition by id, state order by num )
from table
如果减去序列,则这些值对于一行中的值将是恒定的。然后您可以使用窗口功能:
select t.*,
sum(pop) over (partition by state, num - seqnum) as new_population
from (select t.*,
row_number() over (partition by state order by num) as seqnum
from t
) t;