Re: aggregate functions are not allowed in UPDATE - Mailing list pgsql-general

From Alexander Farber
Subject Re: aggregate functions are not allowed in UPDATE
Date
Msg-id CAADeyWiVAsnox5NBp5YMKHVgGT7VR0uACLXUR6O8hWsdQ0xsfQ@mail.gmail.com
Whole thread Raw
In response to Re: aggregate functions are not allowed in UPDATE  ("David G. Johnston" <david.g.johnston@gmail.com>)
Responses Re: aggregate functions are not allowed in UPDATE  ("David G. Johnston" <david.g.johnston@gmail.com>)
List pgsql-general
Unfortunately, I don't understand your advice, David -

On Tue, Jan 15, 2019 at 5:46 PM David G. Johnston <david.g.johnston@gmail.com> wrote:
On Tue, Jan 15, 2019 at 9:42 AM Alexander Farber
<alexander.farber@gmail.com> wrote:
> When I am trying
>
> WITH diffs AS (
>   SELECT
>       gid,
>       uid,
>       played - LAG(played) OVER(PARTITION BY gid ORDER BY played) AS diff
>   FROM moves
>   WHERE uid = 1
> )
> UPDATE users SET
> avg_time = TO_CHAR(AVG(diff), 'HH24:MI')
> FROM diffs;
>
> the syntax error is unfortunately printed by PostgreSQL 10:
>
>     aggregate functions are not allowed in UPDATE

So calculate the average somewhere else, put the result in a column,
and then reference that column in the SET clause.


do you suggest to add a second CTE?

Regards
Alex

pgsql-general by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: aggregate functions are not allowed in UPDATE
Next
From: "David G. Johnston"
Date:
Subject: Re: aggregate functions are not allowed in UPDATE