Armand Violle
Armand Violle

Reputation: 42

Put all the elements of a column into the same array (postgresql)

My question is the following ;

After a first query, I have a table with a single column of bigints, for example :

 id 
----
  1
  2
  3
  4

I would like to convert this column into a postgresql array, which would give - according to the example - {1,2,3,4}.

Any ideas about how to do that ?

Thank you for all your answers and have a nice day, best regards

Upvotes: 0

Views: 34

Answers (1)

user330315
user330315

Reputation:

Use aggregation:

select array_agg(id)
from the_table;

If you need a specific sort order:

select array_agg(id order by id)
from the_table;

Upvotes: 1

Related Questions