UPDATE in a specific order

From: "Luiz K(dot) Matsumura" <luiz(at)planit(dot)com(dot)br>
To: pgsql-sql(at)postgresql(dot)org
Subject: UPDATE in a specific order
Date: 2010-12-16 14:57:52
Message-ID: 4D0A28F0.3020205@planit.com.br
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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.
*
*

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Jasen Betts 2010-12-16 19:58:50 Re: UPDATE in a specific order
Previous Message Jean-David Beyer 2010-12-16 14:38:42 Re: Database consistency after a power shortage