Re: Update table with max occurance from another table

From: Mike Beachy <beachy(at)marketboy(dot)com>
To: Dan Winslow <d(dot)winslow(at)cox(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Update table with max occurance from another table
Date: 2002-11-19 19:19:46
Message-ID: 20021119191946.GA6703@marketdude.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Nov 19, 2002 at 06:27:52PM +0000, Dan Winslow wrote:
> And given the following task :
>
> update a from b such that a.maxtype is set equal to the b.type whose val
> number is the highest for that matching id, that is, the result :
>
> select * from a;
> id | maxtype
> ----+---------
> 1 | type2
> 2 | type1
> (2 rows)
>
> is to be obtained, how can this be accomplished with SQL statements? I am
> looking for a single (perhaps compound ) statement to do it, no procedural
> stuff

How about:

update a set maxtype =
(select b.type from b where b.id = a.id order by b.val desc limit 1)

-mike

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Medi Montaseri 2002-11-19 19:24:53 Re: selecting the last record from a table
Previous Message Stephan Szabo 2002-11-19 19:17:08 Re: Update table with max occurance from another table