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

From David G. Johnston
Subject Re: aggregate functions are not allowed in UPDATE
Date
Msg-id CAKFQuwaN3FoNqjGEzKTaZhO=5d4qR2LgHk-4AF7cQ2DreLNxXg@mail.gmail.com
Whole thread Raw
In response to aggregate functions are not allowed in UPDATE  (Alexander Farber <alexander.farber@gmail.com>)
Responses Re: aggregate functions are not allowed in UPDATE  (Alexander Farber <alexander.farber@gmail.com>)
List pgsql-general
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.

Its also unusual to have FROM with UPDATE but no WHERE clause...

David J.


pgsql-general by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: Refining query statement
Next
From: Alexander Farber
Date:
Subject: Re: aggregate functions are not allowed in UPDATE