Re: sql update max smartries

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Vic Cekvenich <cekvenich(dot)vic(at)portalvu(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: sql update max smartries
Date: 2004-10-23 23:16:32
Message-ID: 20041023231632.GA11915@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Oct 21, 2004 at 18:14:15 -0500,
Vic Cekvenich <cekvenich(dot)vic(at)portalvu(dot)com> wrote:
> I am trying to write a sigle command to update the max number from detail.
>
> Something like:
> update group set max_msgid=max(c.msgid)
> from group g, content c
> where g.id=c.g_id
>
> So group is master, content is detail. I want group to stroe max(msgid)
> from content.

You don't want to put 'group' in the from clause, you will then be joining
'group' with itself (in addition to content).
Also you really don't want to join 'group' and 'content', but rather
'group' and (select c.g_id, max(c.msgid) from content group by g_id).
So the query should look something like (I didn't test this):
UPDATE "group" SET max_msgid=g.msgid
FROM (SELECT c.g_id, max(c.msgid) AS msgid FROM "content" c GROUP BY g_id) AS g
WHERE "group".id = g.g_id;

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bruno Wolff III 2004-10-23 23:25:12 Re: '1 year' = '360 days' ????
Previous Message James Robinson 2004-10-23 22:51:41 COPY data and referential triggers ...