Re: Update table with max occurance from another table

From: Medi Montaseri <medi(dot)montaseri(at)intransa(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 20:37:02
Message-ID: 3DDAA0EE.9070906@intransa.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

add 'as foo' to the end of the nested select...

update a set maxtype = (select bla bla bla desc limit 1 as foo)

Dan Winslow wrote:

>Yes, I tried this, but it doesn't like the order or the limit clause in
>sub-selects.
>
>"Mike Beachy" <beachy(at)marketboy(dot)com> wrote in message
>news:20021119191946(dot)GA6703(at)marketdude(dot)com(dot)(dot)(dot)
>
>
>>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
>>
>>
>>---------------------------(end of broadcast)---------------------------
>>TIP 4: Don't 'kill -9' the postmaster
>>
>>
>
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 1: subscribe and unsubscribe commands go to majordomo(at)postgresql(dot)org
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Doug McNaught 2002-11-19 21:10:26 Re: ssh tunnel problem
Previous Message Daniel C. Wickstrom 2002-11-19 20:19:14 ANNOUNCE: OpenFTS release 0.3.2