Home > Software engineering >  Unnest a two-dimensional array to a table
Unnest a two-dimensional array to a table

Time:06-16

Is it possible to convert the following into a two-columned table?

SELECT * FROM UNNEST([[1,'a'],[2,'b'], [3,'c']) AS tbl (num,str);

Or more generally into a table of n columns where n is the size of the inner array (i.e., the length of a row).

The array structure is arr[rows][cols]

CodePudding user response:

Arrays in PostgreSQL can contain elements of one type (but it can be composite type). So array like array[1,2,'e'] is invalid in PostgreSQL.

What can be done is to unnest two arrays in one statement

SELECT UNNEST(array[1,2,3]),unnest(array['a','b','c']) 
  • Related