Re: STRING_AGG and GROUP BY

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: STRING_AGG and GROUP BY
Date: 2018-03-16 15:40:00
Message-ID: CAKFQuwbi4tkUcF6XmU1+m4JA-Kwsz=h9NTQSovLjy48+rw4KzQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Mar 16, 2018 at 7:17 AM, Alexander Farber <
alexander(dot)farber(at)gmail(dot)com> wrote:

> And I can not change the query to: STRING_AGG(DISTINCT x->>'letter', '')
> AS tiles,
>
> Because for example in the last move with mid=6 the player Bob had played
> 2 tiles, both with letter-value "P" and has formed 2 words (PP and PABCD),
> but adding distinct would suggest he played a single tile "P".
>

​First reaction is to ARRAY_AGG(DISTINCT x) and then write a function that
converts ​that array into a string by extracting 'letter' from each cell in
the array.

Thinking it over a bit you have two columns that both are aggregates but
that are otherwise independent of each other. Since they are independent
they cannot be aggregated at the same time. You need to write a two
subqueries, either in the target list or as separate from/join items, and
then join the already aggregated queries together on their common group by
column.

The presence of DISTINCT here (and, IMO, generally), even if it worked,
would be an indicator that something is not quite right.

David J.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alexander Farber 2018-03-16 16:10:02 Re: STRING_AGG and GROUP BY
Previous Message Adrian Klaver 2018-03-16 15:26:53 Re: error 53200 out of memory