PostgreSQL
 sql >> Base de Dados >  >> RDS >> PostgreSQL

Postgresql - retorna linha inteira como array


Pode ser isso:http://www.sqlfiddle.com/#!1/d41d8 /364
select translate(string_to_array(x.*::text,',')::text,'()','')::text[] 
from pg_tables as x

Como funciona (de dentro para fora), 5 passos:

1º:
select x.*::text from pg_tables as x;

Saída de amostra:
|                                                            X |
----------------------------------------------------------------
|                    (pg_catalog,pg_statistic,postgres,,t,f,f) |
|                         (pg_catalog,pg_type,postgres,,t,f,f) |

2º:
select string_to_array(x.*::text,',') from pg_tables as x;

Saída de amostra:
|                           STRING_TO_ARRAY |
---------------------------------------------
| (pg_catalog,pg_statistic,postgres,,t,f,f) |
|      (pg_catalog,pg_type,postgres,,t,f,f) |

3º:
select string_to_array(x.*::text,',')::text from pg_tables as x;

Saída de amostra:
|                               STRING_TO_ARRAY |
-------------------------------------------------
| {(pg_catalog,pg_statistic,postgres,"",t,f,f)} |
|      {(pg_catalog,pg_type,postgres,"",t,f,f)} |

4º:
select translate( string_to_array(x.*::text,',')::text, '()', '') from pg_tables as x

Saída de amostra:
|                                   TRANSLATE |
-----------------------------------------------
| {pg_catalog,pg_statistic,postgres,"",t,f,f} |
|      {pg_catalog,pg_type,postgres,"",t,f,f} |

Finalmente:
select translate( string_to_array(x.*::text,',')::text, '()', '')::text[] 
from pg_tables as x

Saída de amostra:
|                               TRANSLATE |
-------------------------------------------
| pg_catalog,pg_statistic,postgres,,t,f,f |
|      pg_catalog,pg_type,postgres,,t,f,f |

Teste ao vivo:http://www.sqlfiddle.com/#!1/d41d8/ 373

Para provar que funciona:
with a as 
(
  select translate( string_to_array(x.*::text,',')::text, '()', '')::text[] as colArray 
  from pg_tables as x
)
select row_number() over(), unnest(colArray)
from a;

Saída de amostra:
| ROW_NUMBER |                  UNNEST |
----------------------------------------
|          1 |              pg_catalog |
|          1 |            pg_statistic |
|          1 |                postgres |
|          1 |                         |
|          1 |                       t |
|          1 |                       f |
|          1 |                       f |
|          2 |              pg_catalog |
|          2 |                 pg_type |
|          2 |                postgres |
|          2 |                         |
|          2 |                       t |
|          2 |                       f |
|          2 |                       f |