postgresql 行列互转函数

摘要:
关系数据库的行列转换功能在实际应用中非常常见。Postgresql是一个优秀的开源数据库,需要提供相关的转换功能。

关系型数据库的行列转换函数在实际应用中是相当普遍。postgresql 作为一款优秀的开源数据库,提供相关的转换函数是必须的。

列=》行

使用 string_agg 函数

with tmp_t0 as (
    select 'A'::varchar as c1 union all
    select 'B'::varchar as c1 union all
    select 'C'::varchar as c1 
)
select string_agg(c1,',') 
  from tmp_t0
  ; 

 string_agg 
------------
 A,B,C
(1 row)

使用 array_agg 函数

with tmp_t0 as (
    select 'A'::varchar as c1 union all
    select 'B'::varchar as c1 union all
    select 'C'::varchar as c1 
)
select array_agg(c1) 
  from tmp_t0
  ; 

 array_agg 
-----------
 {A,B,C}
(1 row)

行=》列

使用regexp_split_to_table函数

with tmp_t0 as (
    select 'A,B,C,D'::varchar as c1 
)
select regexp_split_to_table(c1,',') 
  from tmp_t0
  ;

 regexp_split_to_table 
-----------------------
 A
 B
 C
 D
(4 rows)

参考:
https://www.postgresql.org/docs/9.6/static/functions-aggregate.html
https://www.postgresql.org/docs/9.6/static/functions-string.html

免责声明:文章转载自《postgresql 行列互转函数》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Js格式化json字符串Linux 服务器安全加固(等保 2.0 标准)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

配置ogg从Oracle到PostgreSQL的同步复制json数据

标签:goldengate postgresql oracle json 测试环境说明 Oracle:Windows 8.1 + Oracle 12.2.0.1.0 + GoldenGate 12.3.0.1.2 for oracle IP:10.155.4.150 PostgreSQL:CentOS7 + postgresql 10.10-1 + Gol...

Postgresql迁移数据文件存放位置

1. POSTGRESQL的安装 centos7 里面默认的pgsql的版本是 9.2.4 如果想用更高的版本需要执行以下如下的命令 rpm -ivh https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/pgdg-centos11-11-2.noarch.rpm安装成...

Windows如何设置或更改PostgreSQL数据目录位置

[Windows] Step 1:停止PostgreSQL服务 方式一:通过“Windows Services Management?即“服务”中停止相应的PostgreSQL服务 Step 2:修改注册表 对应键值位置在"HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicespostgresql-x64-9....

安装postgreSQL出现configure:error:readline library not found解决方法

要安装 readline , readline-dev 开发包,要么使用 --without-readline 选项关闭 readline 功能。 #yum install readline; #yum install readline-dev; readline 也就是命令行编辑,关闭的话,你直接用psql 就不能编辑命令行,如果输错指令,不能回滚命令历...

greenplum(postgresql) 数据字典

greenplum是基于postgresql开发的分布式数据库,里面大部分的数据字典是一样的。我们在维护gp的时候对gp的数据字典比较熟悉,特此分享给大家。在这里不会详细介绍每个字典的内容,只会介绍常见的应用以及一些已经封装好了的函数。具体的介绍大家可以去看postgresql的中文文档(附件),里面有详细的解释。 1.postgresql中,所有数据库的...

【转】 PostgreSQL数据类型

第六章数据类型 6.1概述 PostgreSQL提供了丰富的数据类型。用户可以使用CREATE TYPE命令在数据库中创建新的数据类型。PostgreSQL的数据类型被分为四种,分别是基本数据类型、复合数据类型、域和伪类型。 基本数据类型是数据库内置的数据类型,包括integer、char、varchar等数据类型。表6-1列出了PostgreSQL提供的...