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

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 标签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数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
1月前
|
编译器 容器
C++string类的介绍及常用函数用法总结
C++string类的介绍及常用函数用法总结
29 1
|
7月前
String.format()函数的简单用法
1.String.format()函数的用法 2.常用转换符 3.常用标识
52 0
|
6月前
|
算法 Java 数据库
抽象类和接口 && String的理解 && StringBuilder和StringBuffer && 异常的基本用法 && 时间复杂度
抽象类和接口 && String的理解 && StringBuilder和StringBuffer && 异常的基本用法 && 时间复杂度
41 0
|
1月前
|
存储 算法 安全
【数据结构与算法初学者指南】【冲击蓝桥篇】String与StringBuilder的区别和用法
【数据结构与算法初学者指南】【冲击蓝桥篇】String与StringBuilder的区别和用法
|
6月前
|
Java
【Java用法】Java中String类型和int类型互转的所有方法
【Java用法】Java中String类型和int类型互转的所有方法
78 0
|
4月前
|
编译器 C++
C++之STL库:string类(用法列举和总结)
C++之STL库:string类(用法列举和总结)
|
9月前
|
关系型数据库 大数据 PostgreSQL
PostgreSQL16-新特性-并行聚合
PostgreSQL16-新特性-并行聚合
102 0
|
4月前
|
C语言 C++ 容器
[C++从入门到精通] 3.string类型的相关用法
[C++从入门到精通] 3.string类型的相关用法
31 0
[C++从入门到精通] 3.string类型的相关用法
|
9月前
|
C语言 C++
C++ string 基本用法
C++ string 基本用法
|
4月前
|
存储 算法 C++
【C++】:string用法详解
【C++】:string用法详解
82 0

相关产品

  • 云原生数据库 PolarDB