Re: sql update max smartries

Поиск
Список
Период
Сортировка
От Bruno Wolff III
Тема Re: sql update max smartries
Дата
Msg-id 20041023231632.GA11915@wolff.to
обсуждение исходный текст
Ответ на sql update max smartries  (Vic Cekvenich <cekvenich.vic@portalvu.com>)
Список pgsql-general
On Thu, Oct 21, 2004 at 18:14:15 -0500,
  Vic Cekvenich <cekvenich.vic@portalvu.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;

В списке pgsql-general по дате отправления:

Предыдущее
От: James Robinson
Дата:
Сообщение: COPY data and referential triggers ...
Следующее
От: Bruno Wolff III
Дата:
Сообщение: Re: '1 year' = '360 days' ????