Sort a text aggregate created with array_agg in postgresql

前端 未结 6 2088
傲寒
傲寒 2021-02-05 04:42

I have a table in postgresql. The following table \"animals\" will do to explain my problem:

name
------
tiger
cat
dog

Now I am using the follo

6条回答
  •  伪装坚强ぢ
    2021-02-05 05:08

    Still, for version 8.4, using the solution suggested by Matthew Wood, if you need to do a grouping in the outer query, the inner query should be sorted, too, for the sorting to be consistent.

    SELECT family, array_agg(animal_name)
    FROM (
        SELECT family, "name" AS animal_name
        FROM animals
        ORDER BY family, "name"
    ) AS sorted_animals
    group by family;
    

提交回复
热议问题