sql - Order-dependant multi-row update in Oracle -
i need update row based on input value
example : in table data this
**lotnumber quantity** 0000001 30 0000002 30 0000003 20 0000004 20 0000005 10
input value -20 then
i need fetch latest lot number , update lot number 0000005 0
, 0000004 10
then output be
**lotnumber quantity** 0000001 30 0000002 30 0000003 20 0000004 10 0000005 0
thanks in advance.
you can combine window functions, ctes , correlated updates achieve this, it's ugly , possibly horribly inefficient. i'm no oracle expert.
update data u set quantity=( d ( select lotnumber l , greatest(0,quantity - greatest (0, 20 + quantity - sum(quantity) on (order lotnumber desc))) v data ) select v d u.lotnumber=d.l );
that "20" in middle input value
fiddle here: http://sqlfiddle.com/#!4/cce2a
Comments
Post a Comment