How convert array elements to rows?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sigra
    New Member
    • Nov 2008
    • 1

    How convert array elements to rows?

    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:
    Code:
    index | element
    ------+--------
        1 |       5
        2 |      77
        3 |      39
        4 |      19
    Which command does this?
  • rski
    Recognized Expert Contributor
    • Dec 2006
    • 700

    #2
    Maybe like that

    Code:
    rski=> select i, (ARRAY[11,22,33])[i] from generate_series(1,array_upper(ARRAY[11,22,33],1)) i;
     i | array
    ---+-------
     1 |    11
     2 |    22
     3 |    33
    (3 rows)
    If you know the dimension of a table you can replace array_upper with it.

    Comment

    Working...