ARRAY_AGG and ORDER

Started by Sterpu Victorover 10 years ago3 messagesgeneral
Jump to latest
#1Sterpu Victor
victor@caido.ro

Hello

I need to order an array using another column in table
ad_query_join_select.
I need something like this but this is not a valid SQL:
SELECT array_to_string(array_agg(aqjs.id ORDER BY aqjs.to_left), ',') AS
str, aq.name
FROM ad_query aq
JOIN ad_query_join_select aqjs ON (aqjs.id_ad_query = aq.id)
GROUP BY aq.id, aq.name;

Is there some equivalent syntax in Postgres?

Thank you.

#2Geoff Winkless
pgsqladmin@geoff.dj
In reply to: Sterpu Victor (#1)
Re: ARRAY_AGG and ORDER

On 26 November 2015 at 12:43, Sterpu Victor <victor@caido.ro> wrote:

Hello

I need to order an array using another column in table ad_query_join_select.

I need something like this but this is not a valid SQL:
SELECT array_to_string(array_agg(aqjs.id *ORDER BY aqjs.to_left*), ',')
AS str, aq.name
FROM ad_query aq
JOIN ad_query_join_select aqjs ON (aqjs.id_ad_query = aq.id)
GROUP BY aq.id, aq.name;

Is there some equivalent syntax in Postgres?

​As per the manual?


http://www.postgresql.org/docs/current/static/sql-expressions.html#SYNTAX-AGGREGATES

​SELECT string_agg(a, ',' ORDER BY a) FROM table;

#3Sterpu Victor
victor@caido.ro
In reply to: Geoff Winkless (#2)
Re: ARRAY_AGG and ORDER

Yes, thank you. :)

------ Original Message ------
From: "Sterpu Victor" <victor@caido.ro>
To: "Geoff Winkless" <pgsqladmin@geoff.dj>
Sent: 11/26/2015 2:51:48 PM
Subject: Re[2]: [GENERAL] ARRAY_AGG and ORDER

Show quoted text

Yes, thank you. :)

------ Original Message ------
From: "Geoff Winkless" <pgsqladmin@geoff.dj>
To: "Sterpu Victor" <victor@caido.ro>
Cc: "PostgreSQL General" <pgsql-general@postgresql.org>
Sent: 11/26/2015 2:46:33 PM
Subject: Re: [GENERAL] ARRAY_AGG and ORDER

On 26 November 2015 at 12:43, Sterpu Victor <victor@caido.ro> wrote:

Hello

I need to order an array using another column in table
ad_query_join_select.
I need something like this but this is not a valid SQL:
SELECT array_to_string(array_agg(aqjs.idORDER BY aqjs.to_left), ',')
AS str, aq.name
FROM ad_query aq
JOIN ad_query_join_select aqjs ON (aqjs.id_ad_query = aq.id)
GROUP BY aq.id, aq.name;

Is there some equivalent syntax in Postgres?

​As per the manual?

http://www.postgresql.org/docs/current/static/sql-expressions.html#SYNTAX-AGGREGATES

​SELECT string_agg(a, ',' ORDER BY a) FROM table;