Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flatten an array of arrays in postgres to a single array

Given something such as the following:

select array[array[1, 2], array[3, 4]]

Which returns

{{1,2},{3,4}}

I would like to know how to flatten this to a single array as:

{1,2,3,4}
like image 248
baxx Avatar asked Dec 22 '25 21:12

baxx


1 Answers

This is one way to achieve it:

select array(select unnest(array[array[1, 2], array[3, 4]]));

Result:

{1,2,3,4}
like image 112
user3738870 Avatar answered Dec 24 '25 11:12

user3738870