I have a table that contains all transactions with Quantity column. Need to calculate the cumulative total of quantity of transaction group and update in each transaction row. TRANSACTION table: id(PK) group_id, txn_id, txn_date, Quantity, cumulative_qty 1 1 111 12-AUG-14 10 10 2 1 112 13-AUG-14 -5 5 3 1 113 14-AUG-14 2 7 4 2 221 23-AUG-14 15 15 5 2 222 23-AUG-14 15 30 I wrote MERGE INTO script and it is working file. But the problem is that it is updating old records also. This table is having huge volume of data and that hit is performance issue. Now I need a merge script that need to fetch only unprocessed new records (cumulative_qty is NULL) and add the Quantity with last cumulative_qty value. Suppose, if there is any new transactions for the same group, the select of merge should pick up old and new records alone and update the new record. id(PK) group_id, txn_id, txn_date, Quantity, cumulative_qty 6 1 114 25-AUG-14 5 NULL 7 1 115 26-AUG-14 2 NULL 8 2 223 25-AUG-14 -10 NULL merge into txn t1 using ( select txn_id, lag(cumulative_qty, 1) over ( partition by group_id order by txn_id ) as prev_cum_qty from txn ) x on (t1.txn_id = x.txn_id) when matched then update set cumulative_qty = quantity + prev_cum_qty where cumulative_qty is null; In the above merge query, the select query returns all 7 rows to process and then merge 2 rows. But I want the select query that should return only 4 rows (id-3,5,6,7,8) i.e. the last cumulative row (order by max date and id) and unprocessed rows only. I am using Oracle 11g. Please help me.