Connecting Tech Pros Worldwide Forums | Help | Site Map

How convert array elements to rows?

Newbie
 
Join Date: Nov 2008
Posts: 1
#1: Nov 3 '08
I need to convert array elements to rows. Suppose that I have the array {5,77,39,19}. I need it as a table with the index in one column and the element in the other column:
Expand|Select|Wrap|Line Numbers
  1. index | element
  2. ------+--------
  3.     1 |       5
  4.     2 |      77
  5.     3 |      39
  6.     4 |      19
Which command does this?

Moderator
 
Join Date: Dec 2006
Location: Europe
Posts: 293
#2: Nov 3 '08

re: How convert array elements to rows?


Maybe like that

Expand|Select|Wrap|Line Numbers
  1. rski=> select i, (ARRAY[11,22,33])[i] from generate_series(1,array_upper(ARRAY[11,22,33],1)) i;
  2.  i | array
  3. ---+-------
  4.  1 |    11
  5.  2 |    22
  6.  3 |    33
  7. (3 rows)
  8.  
If you know the dimension of a table you can replace array_upper with it.
Reply