Simon
Simon

Reputation: 1536

postgresql: how to flatten a table column to text[]

i got a table that have multiple column, let say "id", "name".

is there a function that can flatten a table column to array of text?

 SELECT ARRAY(SELECT row_to_json(t) FROM (SELECT id FROM table) t)

this sql is not what i want due to it produce result

[{"id":1},{"id":2}]

i am looking for a function or query that can produce result as below

[1,2] 

or just simplify, i am looking for a function that can reverse unnest

Upvotes: 0

Views: 1091

Answers (1)

user330315
user330315

Reputation:

Use array_agg()

select array_agg(id)
from table

Note that this will return an array typed with the data type of the column. So if id is an int this will return int[]

Upvotes: 2

Related Questions