Re: update and group by/aggregate

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: update and group by/aggregate
Date: 2008-08-27 10:51:42
Message-ID: 20080827105142.GF7271@frubble.xen.chris-lamb.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Oops, both my statements were horribly broken. They needed a WHERE
clause for the UPDATE.

On Wed, Aug 27, 2008 at 11:44:20AM +0100, Sam Mason wrote:
> UPDATE catalog_items SET authors=array_to_string(x.authors,', ')
> FROM (
> SELECT ia.itemid, array_accum(trim(' \t]' from a.name)) AS authors
> FROM catalog_itemauthor ia
> JOIN catalog_author a ON a.authorid = ia.authorid
> WHERE a.name IS NOT NULL
> AND length(trim(' \t' from a.name))>1
> GROUP BY ia.itemid) x;

should be:

UPDATE catalog_items i SET authors=array_to_string(x.authors,', ')
FROM (
SELECT ia.itemid, array_accum(trim(' \t]' from a.name)) AS authors
FROM catalog_itemauthor ia
JOIN catalog_author a ON a.authorid = ia.authorid
WHERE a.name IS NOT NULL
AND length(trim(' \t' from a.name))>1
GROUP BY ia.itemid) x
WHERE i.itemid = x.itemid;

> UPDATE catalog_items SET authors=array_to_string(x.authors,', ')
> FROM (
> SELECT ia.itemid, array_accum(a.name) AS authors
> FROM catalog_itemauthor ia, (
> SELECT authorid, trim(' \t' from name) AS name
> FROM catalog_author) a
> WHERE ia.authorid = a.authorid
> AND a.name IS NOT NULL
> AND length(a.name) > 1
> GROUP BY ia.itemid) x;

should be:

UPDATE catalog_items i SET authors=array_to_string(x.authors,', ')
FROM (
SELECT ia.itemid, array_accum(a.name) AS authors
FROM catalog_itemauthor ia, (
SELECT authorid, trim(' \t' from name) AS name
FROM catalog_author) a
WHERE ia.authorid = a.authorid
AND a.name IS NOT NULL
AND length(a.name) > 1
GROUP BY ia.itemid) x
WHERE i.itemid = x.itemid;

Sorry!

Sam

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ivan Sergio Borgonovo 2008-08-27 11:19:30 Re: loop vs. aggregate was: update and group by/aggregate
Previous Message Sam Mason 2008-08-27 10:44:20 Re: update and group by/aggregate