In this CTE, each row in mytable
can have 0 or many rows joined to it in jointable
. I'm trying to returning an array_agg
of the jointable
's value
column in this query, but I get an error saying I can't have an aggregate in a RETURNING
.
WITH updated as(
UPDATE mytable SET status = 'A'
FROM
(
SELECT id FROM mytable
WHERE status = 'B'
ORDER BY mycolumn
LIMIT 100
FOR UPDATE
) sub
LEFT JOIN jointable j USING (id)
WHERE mytable.id = sub.id
GROUP BY (mytable.id)
RETURNING mytable.id, array_agg(j.value)
)
select *
from updated
ORDER BY mycolumn
You cannot have a GROUP BY
clause in an UPDATE
statement. Also, the UPDATE
won't necessarily visit all matching rows in the joined table, so it wouldn't be able to return then anyway.
You will have to join jointable
again in the outer query to get the desired result.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.