Re: string_agg distinct order by - Mailing list pgsql-general

From David G. Johnston
Subject Re: string_agg distinct order by
Date
Msg-id CAKFQuwa8j1wT=u__3RCYbqA6RvdvPKJ91b2_kwPtspUdO0pbHQ@mail.gmail.com
Whole thread Raw
In response to string_agg distinct order by  ("Markhof, Ingolf" <ingolf.markhof@de.verizon.com>)
List pgsql-general
On Thu, Aug 19, 2021 at 9:09 AM Markhof, Ingolf <ingolf.markhof@de.verizon.com> wrote:
>
>
> string_agg(distinct status,',' order by status) as list
>
> but this is not what I want: 'green' would get first, red second, yellow last...
>
> I could also drop the distinct and say:
>
> string_agg(status,',' order by val_a) as list
>
> Any pointers?
>

public.function_that_converts_the_array_to_a_string_while_replacing_the_val_a_values_with_their_text_equivalent(array_agg(distinct val_a order by val_a))

Distinct is fairly simple - it sorts the data then skips over duplicates as they appear.  It needs a sort, applied implicitly or explicitly, to put the data in order.  If the sort is explicit it requires you to ensure that the distinct field values are all grouped together (it will do this itself if it adds an implied sort).

David J.

pgsql-general by date:

Previous
From: Rob Sargent
Date:
Subject: Re: Selecting table row with latest date
Next
From: Tom Lane
Date:
Subject: Re: string_agg distinct order by