Re: Adding AVG to a JOIN - Mailing list pgsql-general

From David G. Johnston
Subject Re: Adding AVG to a JOIN
Date
Msg-id CAKFQuwZj0wqBVfKpJfuyhavzvX4CkHP4m-b9AfMKTWHkA9HpBw@mail.gmail.com
Whole thread Raw
In response to Re: Adding AVG to a JOIN  (Alexander Farber <alexander.farber@gmail.com>)
Responses Re: Adding AVG to a JOIN
List pgsql-general
On Mon, Apr 23, 2018 at 12:47 PM, Alexander Farber <alexander.farber@gmail.com> wrote:
                SELECT
                        u.elo,
                        AVG(c.played - c.prev_played) AS avg_time_per_move,
                        (SELECT ROUND(AVG(score), 1) FROM words_moves WHERE uid = u.uid) AS score,

And I don't understand why adding a CTE has caused it, because without the CTE the GROUP BY u.elo was not required...


​Adding "AVG(c.played - c.prev_played)" directly to the top-level select statement​ column list is what turned it into a "GROUP BY" query.  When you embedded the "AVG(score)" in a subquery the GROUP BY was limited to just that subquery, and it had no other columns besides the aggregate and so didn't require a GROUP BY clause.

David J.

pgsql-general by date:

Previous
From: Alexander Farber
Date:
Subject: Re: Adding AVG to a JOIN
Next
From: Tim Cross
Date:
Subject: Re: Postgres and fsync