暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

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

digoal 2018-10-29
1933

作者

digoal

日期

2018-10-29

标签

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

PostgreSQL 许愿链接

您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.

9.9元购买3个月阿里云RDS PostgreSQL实例

PostgreSQL 解决方案集合

德哥 / digoal's github - 公益是一辈子的事.

digoal's wechat

文章转载自digoal,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论