Proposal: array_unique_agg() function

Started by Aleksander Alekseevalmost 4 years ago4 messages
#1Aleksander Alekseev
aleksander@timescale.com

Hi hackers,

I needed an aggregate function similar to array_agg() but which
aggregates only unique values. As it turned out there is no convenient
way of doing this. What I ended up doing instead was aggregating to
JSONB keys and then converting a JSONB object to an array:

SELECT array(select jsonb_object_keys(jsonb_object_agg(mycolumn, true)))
FROM ...

This works but doesn't seem to be the greatest user experience. I
would like to submit a patch that adds array_unique_agg() function
unless anyone has strong objections to this feature.

--
Best regards,
Aleksander Alekseev

#2Pavel Stehule
pavel.stehule@gmail.com
In reply to: Aleksander Alekseev (#1)
Re: Proposal: array_unique_agg() function

Hi

út 1. 3. 2022 v 14:39 odesílatel Aleksander Alekseev <
aleksander@timescale.com> napsal:

Hi hackers,

I needed an aggregate function similar to array_agg() but which
aggregates only unique values. As it turned out there is no convenient
way of doing this. What I ended up doing instead was aggregating to
JSONB keys and then converting a JSONB object to an array:

SELECT array(select jsonb_object_keys(jsonb_object_agg(mycolumn, true)))
FROM ...

This works but doesn't seem to be the greatest user experience. I
would like to submit a patch that adds array_unique_agg() function
unless anyone has strong objections to this feature.

SELECT array_agg(DISTINCT ...) doesn't help?

Regards

Pavel

Show quoted text

--
Best regards,
Aleksander Alekseev

In reply to: Aleksander Alekseev (#1)
Re:Proposal: array_unique_agg() function

Hello

select array_agg(distinct mycolumn) from

from the very beginning? Even the 7.1 manual describes such a syntax: https://www.postgresql.org/docs/7.1/sql-expressions.html

regards, Sergei

#4Aleksander Alekseev
aleksander@timescale.com
In reply to: Sergei Kornilov (#3)
Re: Proposal: array_unique_agg() function

Pavel, Sergei,

SELECT array_agg(DISTINCT ...) doesn't help?

It works, many thanks!

--
Best regards,
Aleksander Alekseev