Return the same row multiple times for each value in a column based on an array

Let's say I have a table with an array column:

id | subIds
1  | {1,2,3}
2  | {4,5}

      

How can I return a result set:

id | subId    
1  | 1
1  | 2
1  | 3
2  | 4
2  | 5

      

... in one request without using a function?

+3


source to share


1 answer


By "without using a function" I assume you mean "without writing my own function to do this".

The function unnest()

will do what you want



select id, unnest(subids) as subid
from the_table;

      

The order in which elements are returned is undefined.

+4


source