Re: string_agg distinct order by

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: "Markhof, Ingolf" <ingolf(dot)markhof(at)de(dot)verizon(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: string_agg distinct order by
Date: 2021-08-19 16:30:00
Message-ID: CAKFQuwa8j1wT=u__3RCYbqA6RvdvPKJ91b2_kwPtspUdO0pbHQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Aug 19, 2021 at 9:09 AM Markhof, Ingolf <
ingolf(dot)markhof(at)de(dot)verizon(dot)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.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2021-08-19 16:30:45 Re: string_agg distinct order by
Previous Message Rob Sargent 2021-08-19 16:27:57 Re: Selecting table row with latest date