Thread: UPDATE in a specific order

UPDATE in a specific order

From
"Luiz K. Matsumura"
Date:
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> 

Re: UPDATE in a specific order

From
Jasen Betts
Date:
> I need to make update of table1 with data on table2 in the order of id
> of table2

that looks like EAV. is it?

> I=B4m trying to do an update like this:

that's not going to work.

perhaps you can rewrite the from part to only return one row for every
table1_fk,  this one row will combine several rows from table2

> The "FROM ( SELECT * FROM table2 ORDER BY id  ) tHist"  is a try to
> force a specific order on table2 to update table1
> but this isn=B4t working.

will only work if the optimiser picks index join on table 1

> There are some way to do this with a UPDATE statement ?

to do it with an update statement you need no more than one rows in the
from for each row in the target.

easiest non update statement approach is probably to use a plpgsql 
function with a loop.

basically you need to find another way to do it.




Re: UPDATE in a specific order

From
"Luiz K. Matsumura"
Date:
Thanks  for reply,<br /><br /> Em 16/12/2010 17:58, Jasen Betts escreveu: <blockquote
cite="mid:iedr1q$dv1$1@reversiblemaps.ath.cx"type="cite"><blockquote type="cite"><pre wrap="">I need to make update of
table1with data on table2 in the order of id
 
of table2
</pre></blockquote><pre wrap="">
that looks like EAV. is it?

</pre></blockquote><br /> Err, I don´t know so much about EAV, so I think that isn´t. I´m just trying to reproduce a
calcin a spreeadsheet. <blockquote cite="mid:iedr1q$dv1$1@reversiblemaps.ath.cx" type="cite"><blockquote
type="cite"><prewrap="">There are some way to do this with a UPDATE statement ?
 
</pre></blockquote><pre wrap="">
to do it with an update statement you need no more than one rows in the
from for each row in the target.

easiest non update statement approach is probably to use a plpgsql 
function with a loop.

basically you need to find another way to do it.

</pre></blockquote><br /> Ok! I make a function in plpgsql to do it.<br /><br /> Thank´s again !<br /><div
class="moz-signature"><br/><b><br /></b></div>