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 20:26:25 |
Message-ID: | CAADeyWjuJ-Ehpnrrhf44E2KTWR2Hk7R3UtqGwod=NZssP3m7ZQ@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Thank you, the following seems to have worked -
On Tue, Jan 15, 2019 at 8:49 PM David G. Johnston <
david(dot)g(dot)johnston(at)gmail(dot)com> wrote:
>
> UPDATE users
> SET avg_time = diffs.average_time_for_the_grouped_by_user
> FROM diffs
> WHERE users.uid = diffs.uid --< the missing "where" I commented about
earlier
>
> But you need to construct the "diffs" CTE/subquery so that it Group[s] By
uid
>
WITH diffs AS (
SELECT
gid,
uid,
played - LAG(played) OVER(PARTITION BY gid ORDER BY played) AS diff
FROM moves
),
avg_diffs AS (
SELECT uid, AVG(diff) as avg_diff FROM diffs GROUP BY uid
)
UPDATE users SET avg_time = avg_diff
FROM avg_diffs
WHERE users.uid = avg_diffs.uid;
https://www.db-fiddle.com/f/w1AYGpoZiWW9bLCYjHDk7H/9
Or did you mean something else?
Regards
Alex
From | Date | Subject | |
---|---|---|---|
Next Message | Daevor The Devoted | 2019-01-16 00:57:27 | Re: Can anyone please provide me list of customers using postgreSQL |
Previous Message | Igor Neyman | 2019-01-15 20:00:28 | RE: Can anyone please provide me list of customers using postgreSQL |