Re: BUG #10972: string_agg function incorrectly concatenating varying delimiter - Mailing list pgsql-bugs

From Marti Raudsepp
Subject Re: BUG #10972: string_agg function incorrectly concatenating varying delimiter
Date
Msg-id CABRT9RA=g1qs7rzy+J6bZG4Zt5QiBeKbfWUhKE78NH2MPbiWsw@mail.gmail.com
Whole thread Raw
In response to BUG #10972: string_agg function incorrectly concatenating varying delimiter  (jeff@goaldriven.com)
List pgsql-bugs
On Wed, Jul 16, 2014 at 3:16 AM,  <jeff@goaldriven.com> wrote:
> Running the query below will show how the delimiter for the current row is
> actually the subsequent rows delimiter.  The sequence goes like this:
>
> TestBlah        1
> ITTest  2
> testfail        3
>
> Yet, the concatened output is this:
>
> "testblah 2 ITtest 3 testfail"

I think you're misunderstanding the point of the 2nd argument. It's
not there for just concatenating two arguments together, it's there to
be a delimiter between the strings being concatenated. Almost always
it should be a constant. For example if you want a result like
"TestBlah,ITTest,testfail", that would be much uglier to do without
the delimiter argument.

The behavior you want is available as simply:
  string_agg(Field1 || ' ' || RowIndex::text, ' ')

Regards,
Marti

pgsql-bugs by date:

Previous
From: John R Pierce
Date:
Subject: Re: BUG #11022: \copy report negative row number
Next
From: Simon Riggs
Date:
Subject: syntax error WITH ORDINALITY