updating with max value

From: "Tamsin" <tg_mail(at)bryncadfan(dot)co(dot)uk>
To: "Postgres General" <pgsql-general(at)postgresql(dot)org>
Subject: updating with max value
Date: 2001-04-26 15:00:45
Message-ID: NEBBKHBOBMJCHDMGKCNJKEAHCNAA.tg_mail@bryncadfan.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

not sure if i'm being stupid...

Here are the tables:

test=# \d order_head
Table "order_head"
Attribute | Type | Modifier
---------------------+---------------+--------------------------
order_head_id | integer | not null
order_edited | date |

test=# \d order_head_hist
Table "order_head_hist"
Attribute | Type | Modifier
---------------------+---------------+----------
order_head_id | integer |
audit_date | timestamp |
audit_action | varchar(10) |

Some of the audit data:

test=# select order_head_id, audit_date from order_head_hist where
audit_action = 'update';
order_head_id | audit_date
---------------+------------------------
1591 | 2001-04-26 14:38:05+01
1589 | 2001-04-26 14:38:05+01
1589 | 2001-04-26 14:38:05+01
1590 | 2001-04-26 14:38:05+01
1590 | 2001-04-26 14:38:05+01
1591 | 2001-04-26 14:38:05+01
1591 | 2001-04-26 14:38:05+01
1591 | 2001-04-26 14:38:05+01

But when I do this:

test=# UPDATE order_head SET
test-# order_edited = max(order_head_hist.audit_date)
test-# FROM order_head_hist
test-# WHERE order_head.order_head_id = order_head_hist.order_head_id
test-# AND order_head_hist.audit_action = 'update' ;
UPDATE 1
test=#

It only updates one row:

test=# select order_head_id, order_edited from order_head;
order_head_id | order_edited
---------------+--------------
1590 |
1591 |
1589 | 2001-04-26

I'm guessing its just updating the row with the max audit_date in the whole
order_head_hist table, rather than grouping by order_head_id, but i can't
seem to add a group by. Is there a way round this or am i missing something
obvious...?

thanks for any help
tamsin

PostgreSQL 7.0.2

Browse pgsql-general by date

  From Date Subject
Next Message Michelle Murrain 2001-04-26 15:07:33 Re: I am now Linux and PostgreSQL user, have a question
Previous Message Peter Eisentraut 2001-04-26 14:58:28 Re: Inheritance in 7.1