UPDATE in a specific order - Mailing list pgsql-sql

From Luiz K. Matsumura
Subject UPDATE in a specific order
Date
Msg-id 4D0A28F0.3020205@planit.com.br
Whole thread Raw
List pgsql-sql
Hi,<br /><br /> I have a follow scenario:<br /><br /> CREATE TABLE table1 (<br />   id integer<br /> , vlpr
numeric(10,2)<br/> , vlab numeric(10,2)<br /> , vlbx  numeric(15,5)<br /> , pct  numeric(12,8)<br /> );<br /><br />
CREATETABLE table2 (<br />   id integer<br /> ,  fk_table1 integer<br /> , tpop char(2)<br /> ,  valor 
numeric(15,5)<br/> );<br /><br /> insert into table1  VALUES ( 1, 200 , 0 , 0 , 1 );<br /><br /> insert into table2 
VALUES<br />   ( 1, 1 , 'CR'  ,   100  )<br /> , ( 2, 1 , 'BX'  ,     15  )<br /> , ( 3, 1 , 'AC'  ,     40  );<br
/><br/> I need to make update of table1 with data on table2 in the order of id of table2<br /><br /> I´m trying to do
anupdate like this: <br /><br /> UPDATE table1<br /> SET  vlab = vlab + CASE WHEN tHist.tpop IN ('BX' , 'DC') <br />
           THEN - tHist.valor <br />            ELSE tHist.valor <br />            END<br />    , vlbx = vlbx + CASE
WHENtHist.tpop IN ('BX', 'DC') <br />                         THEN tHist.valor <br />                    ELSE 0 <br />
                  END<br />    , pct = CASE WHEN tHist.tpop in ('AC',  'DC' ) <br />                 THEN (vlpr - vlbx)
/vlab  <br />            ELSE pct <br />            END<br /> FROM  ( SELECT * FROM table2 ORDER BY id  ) tHist<br />
WHEREtable1.id = tHist.fk_table1<br /><br /><br /> The "FROM ( SELECT * FROM table2 ORDER BY id  ) tHist"  is a try to
forcea specific order on table2 to update table1<br /> but this isn´t working.<br /><br /> There are some way to do
thiswith a UPDATE statement ?<br /><br /> Thanks in advance,<br /><br /><div class="moz-signature">-- <br /> Luiz K.
Matsumura<br/> Plan IT Tecnologia Informática Ltda.<br /><b><br /></b></div> 

pgsql-sql by date:

Previous
From: Jean-David Beyer
Date:
Subject: Re: Database consistency after a power shortage
Next
From: Jasen Betts
Date:
Subject: Re: UPDATE in a specific order