Re: Adding AVG to a JOIN

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: Adding AVG to a JOIN
Date: 2018-04-23 19:58:39
Message-ID: CAKFQuwZj0wqBVfKpJfuyhavzvX4CkHP4m-b9AfMKTWHkA9HpBw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Apr 23, 2018 at 12:47 PM, Alexander Farber <
alexander(dot)farber(at)gmail(dot)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.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tim Cross 2018-04-23 22:05:04 Re: Postgres and fsync
Previous Message Alexander Farber 2018-04-23 19:47:26 Re: Adding AVG to a JOIN