表"动物":

animal_name animal_type
Tom         Cat
Jerry       Mouse
Kermit      Frog

查询:

SELECT 
array_to_string(array_agg(animal_name),';') animal_names,
array_to_string(array_agg(animal_type),';') animal_types
FROM animals;

预期结果:

Tom;Jerry;Kerimt, Cat;Mouse;Frog
OR
Tom;Kerimt;Jerry, Cat;Frog;Mouse

我能确定第一个聚合函数的顺序和第二个聚合函数的顺序是一样的吗.

Tom;Jerry;Kermit, Frog;Mouse,Cat

推荐答案

If you are on a PostgreSQL version < 9.0 then:

起始:http://www.postgresql.org/docs/8.4/static/functions-aggregate.html

在当前的实现中,原则上未指定输入的顺序.然而,从排序的子查询中提供输入值通常是可行的.例如:

Select xmlagg(x)FROM(y DESC从测试订单中 Select x)作为选项卡;

所以在你的情况下,你会写:

SELECT
array_to_string(array_agg(animal_name),';') animal_names,
array_to_string(array_agg(animal_type),';') animal_types
FROM (SELECT animal_name, animal_type FROM animals) AS x;

然后,数组_agg的输入将是无序的,但在两列中都是相同的.如果愿意,可以在子查询中添加ORDER BY子句.

Postgresql相关问答推荐

如何在查询空字段时设置pgtype.comb为默认值?

横向联接返回的行数太多

如何使用docker/docker-compose转到本地主机?

Postgres:这是对OVERLAPS谓词的等效重写吗?

使用 pgAdmin 4 v7.4 在 Windows 11 上全新安装 PostgreSQL 15.3-3 无法启动 - 卡在正在加载 pgAdmin 4 v7.4...

postgres 中的模式前缀,被调用元素的范围

联合所有 postgresql Select 子句保留顺序

Heroku Rails 4 无法连接到服务器:connection refused

PostgreSQL 查询到 Excel 表

postgres 和 python

从左连接更新 Postgres

如何从 WSL 连接到 windows postgres 数据库

Postgres 类似于 SQL Server 中的 CROSS APPLY

Entity Framework Core jsonb 列类型

SQLAlchemy 声明式:定义触发器和索引 (Postgres 9)

Postgresql滚动删除旧行?

在布尔字段上添加索引

psql 将默认 statement_timeout 设置为 postgres 中的用户

如何在 JPA 中使用 Postgres JSONB 数据类型?

postgresql DB中唯一键的正确数据类型是什么?