PostgreSQL array_agg 顺序

发布于 2024-12-03 06:44:28 字数 511 浏览 0 评论 0原文

表“动物”:

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

Table 'animals':

animal_name animal_type
Tom         Cat
Jerry       Mouse
Kermit      Frog

Query:

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

Expected result:

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

Can I be sure that order in first aggregate function will always be the same as in second.
I mean I would't like to get:

Tom;Jerry;Kermit, Frog;Mouse,Cat

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一个人练习一个人 2024-12-10 06:44:29

使用 ORDER BY,就像手册中的示例:

SELECT array_agg(a ORDER BY b DESC) FROM table;

Use an ORDER BY, like this example from the manual:

SELECT array_agg(a ORDER BY b DESC) FROM table;
千仐 2024-12-10 06:44:29

如果您使用的是 PostgreSQL 版本 9.0 然后:

来自:http://www.postgresql.org/docs/8.4 /static/functions-aggregate.html

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

从(SELECT x FROM test ORDER BY y DESC)AS 选项卡中选择 xmlagg(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;

array_agg 的输入将是无序的,但两列中的输入都是相同的。如果您愿意,可以向子查询添加一个 ORDER BY 子句。

If you are on a PostgreSQL version < 9.0 then:

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

In the current implementation, the order of the input is in principle unspecified. Supplying the input values from a sorted subquery will usually work, however. For example:

SELECT xmlagg(x) FROM (SELECT x FROM test ORDER BY y DESC) AS tab;

So in your case you would write:

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;

The input to the array_agg would then be unordered but it would be the same in both columns. And if you like you could add an ORDER BY clause to the subquery.

灯下孤影 2024-12-10 06:44:29

根据 Tom Lane 的说法:

...如果我没读错的话,OP 希望确保两个聚合函数将以*相同*未指定的顺序查看数据。我认为这是一个非常安全的假设。服务器必须不遗余力地做不同的事情,但它没有。

...因此记录下来的行为是,没有自己的 ORDER BY 的聚合将以 FROM 子句提供的任何顺序查看行。

因此,我认为可以假设查询中的所有聚合(其中没有一个使用 ORDER BY)将以相同的顺序看到输入数据。但顺序本身未指定(这取决于 FROM 子句提供行的顺序)。

来源:PostgreSQL 邮件列表

According to Tom Lane:

... If I read it right, the OP wants to be sure that the two aggregate functions will see the data in the *same* unspecified order. I think that's a pretty safe assumption. The server would have to go way out of its way to do differently, and it doesn't.

... So it is documented behavior that an aggregate without its own ORDER BY will see the rows in whatever order the FROM clause supplies them.

So I think it's fine to assume that all the aggregates, none of which uses ORDER BY, in your query will see input data in the same order. The order itself is unspecified though (which depends on the order the FROM clause supplies rows).

Source: PostgreSQL mailing list

淡忘如思 2024-12-10 06:44:29

这样做:

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

Do this:

SELECT 
    array_to_string(array_agg(animal_name order by animal_name),';') animal_names,
    array_to_string(array_agg(animal_type order by animal_type),';') animal_types
FROM 
    animals;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文