PostgreSQL listagg within group (order by) 聚合兼容用法 string_agg ( order by) - 行列变换,CSV构造...

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
简介: 标签PostgreSQL , order-set agg , listagg , string_agg , order背景listagg — Rows to Delimited StringsThe listagg function transforms values from a g...

标签

PostgreSQL , order-set agg , listagg , string_agg , order


背景

listagg — Rows to Delimited Strings

The listagg function transforms values from a group of rows into a list of values that are delimited by a configurable separator. Listagg is typically used to denormalize rows into a string of comma-separated values (CSV) or other comparable formats suitable for human reading.

Listagg does not apply any escaping: it is not generally possible to tell whether an occurrence of the separator in the result is an actual separator, or just part of a value. The safe use of listagg for electronic data interfaces is therefore limited to cases in which an unambiguous separator can be selected, e.g. when aggregating numbers, dates, or strings that are known to not contain the separator.

When implementing electronic data interfaces, arrays and document types (JSON, XML) are advantageous as they offer type safety, or at least proper escaping.

PostgreSQL string_agg

string_agg 代替listagg,实现同样功能。

建表

postgres=# create table tbl1 (gid int, val text, ts timestamp default clock_timestamp());  
CREATE TABLE  

写入测试数据

postgres=# insert into tbl1 values (1,'a'),(1,'b'),(1,null),(2,'test'),(2,'a""b"c'),(3,'fw');  
INSERT 0 6  

数据

postgres=# select * from tbl1;  
 gid |  val   |             ts               
-----+--------+----------------------------  
   1 | a      | 2018-10-29 21:00:24.593859  
   1 | b      | 2018-10-29 21:00:24.593994  
   1 |        | 2018-10-29 21:00:24.593997  
   2 | test   | 2018-10-29 21:00:24.593998  
   2 | a""b"c | 2018-10-29 21:00:24.594  
   3 | fw     | 2018-10-29 21:00:24.594001  
(6 rows)  

逆向聚合,双引号作为quote字符,转义文本内的双引号,空值使用NULL表示。

postgres=# select gid, string_agg(coalesce('"'||replace(val,'"','\"')||'"','NULL'),',' order by ts desc) from tbl1 group by gid;  
 gid |     string_agg       
-----+--------------------  
   1 | NULL,"b","a"  
   2 | "a\"\"b\"c","test"  
   3 | "fw"  
(3 rows)  

正向聚合,双引号作为quote字符,转义文本内的双引号,空值使用NULL表示。

postgres=# select gid, string_agg(coalesce('"'||replace(val,'"','\"')||'"','NULL'),',' order by ts) from tbl1 group by gid;  
 gid |     string_agg       
-----+--------------------  
   1 | "a","b",NULL  
   2 | "test","a\"\"b\"c"  
   3 | "fw"  
(3 rows)  

正向聚合,不使用QUOTE,直接去除NULL值

postgres=# select gid, string_agg(val,',' order by ts) from tbl1 group by gid;  
 gid | string_agg    
-----+-------------  
   1 | a,b  
   2 | test,a""b"c  
   3 | fw  
(3 rows)  

order by 任意字段、表达式、转换

order by可以任意字段、表达式、类型转换

select gid, string_agg(val,',' order by xx::numeric) from tbl1 group by gid;

select gid, string_agg(val,',' order by abs(xxx)) from tbl1 group by gid;

select gid, string_agg(val,',' order by mod(x,5),xxxx) from tbl1 group by gid;
postgres=# create table tbl(id int, c1 text);
CREATE TABLE
postgres=# insert into tbl values (1,'1'),(2,'12'),(3,'2');
INSERT 0 3

postgres=# select string_agg(c1,',' order by c1::numeric) from tbl;
 string_agg 
------------
 1,2,12
(1 row)

postgres=# select string_agg(c1,',' order by c1) from tbl;
 string_agg 
------------
 1,12,2
(1 row)

参考

《PostgreSQL 聚合表达式 FILTER , order , within group, over window 用法》

《PostgreSQL aggregate function 3 : Aggregate Functions for Ordered-Set》

https://modern-sql.com/feature/listagg

https://www.postgresql.org/docs/11/static/functions-aggregate.html

https://wiki.postgresql.org/wiki/PostgreSQL_vs_SQL_Standard

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
6月前
|
存储 Java
构造String问题之构造一个Trusted MethodHandles.Lookup实例,如何实现
构造String问题之构造一个Trusted MethodHandles.Lookup实例,如何实现
|
6月前
|
存储 Java
构造String问题之在JDK 9及更高版本中,直接访问String对象的coder和value属性,如何实现
构造String问题之在JDK 9及更高版本中,直接访问String对象的coder和value属性,如何实现
|
8月前
|
C++ 容器
C++字符串string容器(构造、赋值、拼接、查找、替换、比较、存取、插入、删除、子串)
C++字符串string容器(构造、赋值、拼接、查找、替换、比较、存取、插入、删除、子串)
105 1
|
8月前
|
算法 Linux C语言
7.学习STL和string类:版本、组件、构造、操作及应用
7.学习STL和string类:版本、组件、构造、操作及应用
|
9月前
|
Java 开发者
干货总结|快速构造String对象及访问其内部成员的技巧
本文详细解释了String类的底层实现,介绍了构造String对象及其访问其内部成员的技巧。
|
关系型数据库 大数据 PostgreSQL
PostgreSQL16-新特性-并行聚合
PostgreSQL16-新特性-并行聚合
163 0
|
9月前
|
SQL 关系型数据库 PostgreSQL
PostgreSQL【SQL 01】根据条件更新字段值或追加信息STRPOS(string, substring)函数使用及LIKE函数对比
PostgreSQL【SQL 01】根据条件更新字段值或追加信息STRPOS(string, substring)函数使用及LIKE函数对比
251 0
|
SQL 关系型数据库 PostgreSQL
PostgreSQL中E‘string‘ 的使用
在PostgreSQL中,E'string' 是一种特殊的字符串表示方式,其中的E代表"ESCAPE STRING",即转义字符串。 使用E表示法时,可以在字符串中使用转义字符来表示特殊字符,如换行符(\n),制表符(\t),反斜杠(\\),等等。这种语法可以帮助我们表示那些在普通字符串中可能会引起语法错误或不易识别的特殊字符。 下面是一些使用E'string'的示例及其使用场景: 1. 转义特殊字符:字符串中包含双引号和单引号。 ```sql SELECT E'"Hello" said the \'world\''; ``` 输出结果: "Hello" said the 'world'
389 0
|
存储 SQL 监控
16PostgreSQL 本地分区表的用法和优化|学习笔记
快速学习16PostgreSQL 本地分区表的用法和优化
924 0
16PostgreSQL 本地分区表的用法和优化|学习笔记
|
SQL 安全 关系型数据库
17PostgreSQL shared nothing分布式用法讲解|学习笔记(三)
快速学习17PostgreSQL shared nothing分布式用法讲解
293 0
17PostgreSQL shared nothing分布式用法讲解|学习笔记(三)

相关产品

  • 云原生数据库 PolarDB