geomaps9
geomaps9

Reputation: 37

How to turn a column of ints into one array in postgres

I currently have a table with one column and 400 rows; each row has an integer. How can I create an int array with all of these integers that preserves order?

I am using postgreSQL-9.2.

Upvotes: 1

Views: 721

Answers (2)

Walker Farrow
Walker Farrow

Reputation: 3875

SELECT array_agg(column_name ORDER by sort_column_name) AS ints
FROM table

Upvotes: 0

user330315
user330315

Reputation:

select array_agg(int_column order by some_column) as int_array_column
from the_table;

Where some_column is the column that defines the "order" of the integer values. Rows in relational database do not have "an order", so your request "that preserves order" only makes sense if you have a column that defines that sort order that you try to preserve.

Upvotes: 1

Related Questions