PostgreSQL
 sql >> Datenbank >  >> RDS >> PostgreSQL

Postgresql - gibt die gesamte Zeile als Array zurück

Könnte dies sein:http://www.sqlfiddle.com/#!1/d41d8 /364

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

Wie es funktioniert (von innen nach außen), 5 Schritte:

1.:

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

Beispielausgabe:

|                                                            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;

Beispielausgabe:

|                           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;

Beispielausgabe:

|                               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

Beispielausgabe:

|                                   TRANSLATE |
-----------------------------------------------
| {pg_catalog,pg_statistic,postgres,"",t,f,f} |
|      {pg_catalog,pg_type,postgres,"",t,f,f} |

Zum Schluss:

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

Beispielausgabe:

|                               TRANSLATE |
-------------------------------------------
| pg_catalog,pg_statistic,postgres,,t,f,f |
|      pg_catalog,pg_type,postgres,,t,f,f |

Live-Test:http://www.sqlfiddle.com/#!1/d41d8/ 373

Um zu beweisen, dass es funktioniert:

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;

Beispielausgabe:

| 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 |