Re: STRING_AGG and GROUP BY - Mailing list pgsql-general

From Alexander Farber
Subject Re: STRING_AGG and GROUP BY
Date
Msg-id CAADeyWjAc21emBjQUV0Uv-xWXAhZAmT=L+X1TZV5p5V3zNwqGw@mail.gmail.com
Whole thread Raw
In response to Re: STRING_AGG and GROUP BY  ("David G. Johnston" <david.g.johnston@gmail.com>)
Responses Re: STRING_AGG and GROUP BY  ("David G. Johnston" <david.g.johnston@gmail.com>)
List pgsql-general
Hi David -

On Fri, Mar 16, 2018 at 4:40 PM, David G. Johnston <david.g.johnston@gmail.com> wrote:
​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.


thank you for confirming my feeling that DISTINCT is a bad indicator here...

But you say that "tiles" and  "word (score)" are unrelated and this does not seem true to me:

For each move id aka "mid" there is a JSON value, describing how the player played the letter tiles.
And for the same "mid" there is a list of one or more "word (score)"s achieved...

Maybe it is possible to GROUP BY tiles somehow or some kind of special SQL JOIN?

Regards
Alex

pgsql-general by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: STRING_AGG and GROUP BY
Next
From: "David G. Johnston"
Date:
Subject: Re: STRING_AGG and GROUP BY