sql >> Databasteknik >  >> RDS >> PostgreSQL

Postgresql - returnera hela raden som array

Kan vara detta:http://www.sqlfiddle.com/#!1/d41d8 /364

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

Hur det fungerar (inifrån och ut), 5 steg:

1:a:

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

Exempelutgång:

|                                                            X |
----------------------------------------------------------------
|                    (pg_catalog,pg_statistic,postgres,,t,f,f) |
|                         (pg_catalog,pg_type,postgres,,t,f,f) |

2:a:

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

Exempelutgång:

|                           STRING_TO_ARRAY |
---------------------------------------------
| (pg_catalog,pg_statistic,postgres,,t,f,f) |
|      (pg_catalog,pg_type,postgres,,t,f,f) |

3:a:

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

Exempelutgång:

|                               STRING_TO_ARRAY |
-------------------------------------------------
| {(pg_catalog,pg_statistic,postgres,"",t,f,f)} |
|      {(pg_catalog,pg_type,postgres,"",t,f,f)} |

4:e:

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

Exempelutgång:

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

Till sist:

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

Exempelutgång:

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

Livetest:http://www.sqlfiddle.com/#!1/d41d8/ 373

För att bevisa att det fungerar:

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;

Exempelutdata:

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


  1. Implementering av failover i MS SQL Server 2017 Standard

  2. SQL pivotera kolumnvärdena

  3. FEL:slut på minne på maskin med 32 GB RAM och utan växlingsfil

  4. ORACLE PROCEDUR - AUTHID tillåts endast på schemanivå