
February 2nd, 2012, 10:58 AM
|
|
Contributing User
|
|
Join Date: Oct 2003
Location: Germany
|
|
You don't need to update the data, you can simply retrieve it with a single statement:
Code:
select id,
amount,
the_date,
lag(amount) over (partition by id order by the_date) as prev_amount
from the_table
order by id, the_date;
Now if you insist on updating it, this would should it:
Code:
merge into the_table t1
using (
select id,
amount,
the_date,
lag(amount) over (partition by id order by the_date) as prev_amount
from the_table
) t2 on (t1.id = t2.id and t1.amount is null)
when matched then
update
set t1.amount = t2.prev_amount;
(Not tested)
Last edited by shammat : February 2nd, 2012 at 02:28 PM.
|