By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,739 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

How convert array elements to rows?

P: 1
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?
Nov 3 '08 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 700
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.
Nov 3 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.