Re: update and group by/aggregate - Mailing list pgsql-general

From Sam Mason
Subject Re: update and group by/aggregate
Date
Msg-id 20080827105142.GF7271@frubble.xen.chris-lamb.co.uk
Whole thread Raw
In response to Re: update and group by/aggregate  (Sam Mason <sam@samason.me.uk>)
List 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

pgsql-general by date:

Previous
From: Sam Mason
Date:
Subject: Re: update and group by/aggregate
Next
From: Ivan Sergio Borgonovo
Date:
Subject: Re: loop vs. aggregate was: update and group by/aggregate