【发布时间】:2015-10-13 14:57:45
【问题描述】:
我在 postgresql-9.4.2 中有一个带有数组列的表。
create schema test_schema;
create table test_schema.test_table;
(
array_column int[]
);
insert into test_schema.test_table values
(ARRAY[5,12,6,2]),
(ARRAY[51,4,2]),
(ARRAY[2]),
(ARRAY[3,16]);
看起来像这样
| array_column |
| integer[] |
--------------------
1 | {5,12,6,2} |
2 | {51,4,2} |
3 | {2} |
4 | {3,16} |
我想查询这张表,并在我的查询中添加一列,这是一个从 1 计数到数组大小的数组。
我已经弄清楚如何创建一个大小相同的数组,如下所示
select
array_column,
array_fill(1,array[array_length(array_column,1)],array[1]) as counter
from
test_schema.test_table;
返回以下结果
| array_column | counter |
| integer[] | integer[] |
---------------------------------
1 | {5,12,6,2} | {1,1,1,1} |
2 | {51,4,2} | {1,1,1} |
3 | {2} | {1} |
4 | {3,16} | {1,1} |
我打算采用计数器的滚动总和,这会给我想要的结果,但我不知道该怎么做。
这是想要的结果:
| array_column | counter |
| integer[] | integer[] |
---------------------------------
1 | {5,12,6,2} | {1,2,3,4} |
2 | {51,4,2} | {1,2,3} |
3 | {2} | {1} |
4 | {3,16} | {1,2} |
感谢您的帮助
【问题讨论】:
标签: sql postgresql psql postgresql-9.4