sql - Returning the results of hundreds of columns into an array -


i have table hundreds of columns. need take result of every column (except one) , put them array , bring rest of results. here table looks like:

id          x123     x124    x125    x126  ...... 2323343     0        0       0       1 3434566     1        1       1       0 3434342     1        1       0       0  3366577     0        1       1       1 ....        ....     ....    ....    .... 

this table continues on while. need of x# column's results brought in array rest of tables results (except id column). results like:

array            x123     x124    x125    x126  ...... {0,0,0,1,...}    0        0       0       1 {1,1,1,0,...}    1        1       1       0 {1,1,0,0,...}    1        1       0       0  {0,1,1,1,...}    0        1       1       1 ....             ....     ....    ....    .... 

my current sql statement this:

select * mffcu.crosstab_183 

i figure take function of sort build table these results , fine. don't know begin getting every column , every record thrown array right without naming every single column (there many). swing in right direction greatfully.

if format of table simple , strict seems (the first column consists of 7 digits), resort a simple trick:

select string_to_array(right(left(t::text, -1), -9), ',')   mffcu.crosstab_183 t; 

that's all.
left() , right() require postgresql 9.1 or above. older versions:

select string_to_array(substring(rtrim(t::text, ')'), 10), ',')   mffcu.crosstab_183 t; 

explain

every type can cast text in postgres, includes composite , row types. so

  1. cast whole row text.
  2. remove enclosing parentheses , first column - in case identified length.
  3. convert result array string_to_array().

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -