Re: aggregate functions are not allowed in UPDATE

From: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
To:
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: aggregate functions are not allowed in UPDATE
Date: 2019-01-15 19:42:15
Message-ID: CAADeyWgOszPLJAsafV6FjH3dwKxXv8Ow07PcVQQ+xUrb5+p=-A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Last question please - how to run the query for all users?

I know I could use the FOR-loop from PL/PgSQL, but is there also a pure SQL
way?

How to refer to the outside "uid" from inside the CTE in the query below?

WITH diffs AS (
SELECT
gid,
uid,
played - LAG(played) OVER(PARTITION BY gid ORDER BY played) AS diff
FROM moves
WHERE uid = how_to_refer_to_the_outside_uid
)
UPDATE users SET avg_time =
(
SELECT
AVG(diff)
FROM diffs
GROUP BY uid
)
WHERE uid IN (SELECT uid FROM users);

https://www.db-fiddle.com/f/w1AYGpoZiWW9bLCYjHDk7H/2

Regards
Alex

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ramamoorthi, Meenakshi 2019-01-15 19:42:19 Can anyone please provide me list of customers using postgreSQL
Previous Message Alexander Farber 2019-01-15 19:23:33 Re: aggregate functions are not allowed in UPDATE