Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate multiple arrays in Postgres

Tags:

postgresql

Is there a way to concatenate multiple arrays into one array in Postgres?

For example, something like this:

ARRAY_CAT(
    ARRAY_FILL(5, ARRAY[4]),
    ARRAY_FILL(2, ARRAY[3]),
    ARRAY_FILL(11, ARRAY[3])
)

For this example, I'd like to see an output of

[5,5,5,5,2,2,2,11,11,11]
like image 303
Ron M Avatar asked Aug 18 '17 19:08

Ron M


People also ask

What is Unnest in PostgreSQL?

Unnest function generates a table structure of an array in PostgreSQL. Unnest array function is beneficial in PostgreSQL for expanding the array into the set of values or converting the array into the structure of the rows. PostgreSQL offers unnest() function.

What is Array_agg in Postgres?

PostgreSQL ARRAY_AGG() function is an aggregate function that accepts a set of values and returns an array where each value in the input set is assigned to an element of the array. Syntax: ARRAY_AGG(expression [ORDER BY [sort_expression {ASC | DESC}], [...])

How do I select an array in PostgreSQL?

PostgreSQL allows us to define a table column as an array type. The array must be of a valid data type such as integer, character, or user-defined types. To insert values into an array column, we use the ARRAY constructor.

Which of the following will be used to search data in the array in PostgreSQL?

To find PostgreSQL array elements using where clause To filter the rows, we can use the array element in the WHERE clause as the condition. In the following example, we will use the below command to identify those people who has the mobile_number (308)-589-23458 as the second mobile number: SELECT person_name.


1 Answers

Use the || concatenation operator

select 
    array_fill(5, array[4]) ||
    array_fill(2, array[3]) ||
    array_fill(11, array[3])
like image 66
Clodoaldo Neto Avatar answered Oct 08 '22 10:10

Clodoaldo Neto