pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
UPDATE in a specific order
3+ messages / 2 participants
[nested] [flat]

* UPDATE in a specific order
@ 2010-12-16 14:57 Luiz K. Matsumura <luiz@planit.com.br>
  2010-12-16 19:58 ` Re: UPDATE in a specific order Jasen Betts <jasen@xnet.co.nz>
  0 siblings, 1 reply; 3+ messages in thread

From: Luiz K. Matsumura @ 2010-12-16 14:57 UTC (permalink / raw)
  To: pgsql-sql

Hi,

I have a follow scenario:

CREATE TABLE table1 (
   id integer
, vlpr numeric(10,2)
, vlab numeric(10,2)
, vlbx  numeric(15,5)
, pct  numeric(12,8)
);

CREATE TABLE table2 (
   id integer
,  fk_table1 integer
, tpop char(2)
,  valor  numeric(15,5)
);

insert into table1  VALUES ( 1, 200 , 0 , 0 , 1 );

insert into table2  VALUES
   ( 1, 1 , 'CR'  ,   100  )
, ( 2, 1 , 'BX'  ,     15  )
, ( 3, 1 , 'AC'  ,     40  );

I need to make update of table1 with data on table2 in the order of id 
of table2

I´m trying to do an update like this:

UPDATE table1
SET  vlab = vlab + CASE WHEN tHist.tpop IN ('BX' , 'DC')
             THEN - tHist.valor
            ELSE tHist.valor
            END
    , vlbx = vlbx + CASE WHEN tHist.tpop IN ('BX', 'DC')
                         THEN tHist.valor
                    ELSE 0
                    END
    , pct = CASE WHEN tHist.tpop in ('AC',  'DC' )
                 THEN (vlpr - vlbx) / vlab
            ELSE pct
            END
FROM  ( SELECT * FROM table2 ORDER BY id  ) tHist
WHERE table1.id = tHist.fk_table1


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´t working.

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

Thanks in advance,

-- 
Luiz K. Matsumura
Plan IT Tecnologia Informática Ltda.
*
*

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: UPDATE in a specific order
  2010-12-16 14:57 UPDATE in a specific order Luiz K. Matsumura <luiz@planit.com.br>
@ 2010-12-16 19:58 ` Jasen Betts <jasen@xnet.co.nz>
  2010-12-17 19:01   ` Re: UPDATE in a specific order Luiz K. Matsumura <luiz@planit.com.br>
  0 siblings, 1 reply; 3+ messages in thread

From: Jasen Betts @ 2010-12-16 19:58 UTC (permalink / raw)
  To: pgsql-sql


> 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.





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: UPDATE in a specific order
  2010-12-16 14:57 UPDATE in a specific order Luiz K. Matsumura <luiz@planit.com.br>
  2010-12-16 19:58 ` Re: UPDATE in a specific order Jasen Betts <jasen@xnet.co.nz>
@ 2010-12-17 19:01   ` Luiz K. Matsumura <luiz@planit.com.br>
  0 siblings, 0 replies; 3+ messages in thread

From: Luiz K. Matsumura @ 2010-12-17 19:01 UTC (permalink / raw)
  To: pgsql-sql; jasen@xnet.co.nz

Thanks  for reply,

Em 16/12/2010 17:58, Jasen Betts escreveu:
>> I need to make update of table1 with data on table2 in the order of id
>> of table2
> that looks like EAV. is it?
>

Err, I don´t know so much about EAV, so I think that isn´t. I´m just 
trying to reproduce a calc in a spreeadsheet.
>> 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.
>

Ok! I make a function in plpgsql to do it.

Thank´s again !

*
*

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2010-12-17 19:01 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-12-16 14:57 UPDATE in a specific order Luiz K. Matsumura <luiz@planit.com.br>
2010-12-16 19:58 ` Jasen Betts <jasen@xnet.co.nz>
2010-12-17 19:01   ` Luiz K. Matsumura <luiz@planit.com.br>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox