Anitha
Anitha

Reputation: 77

Oracle: insert new row if previous row values are different from current row values. Else, update

In the PLSQL,I have a table A with three columns: product, amount, date.

Table A:

 Product-  Amount- Date

1S        100     1/1/2005
1S        100     1/2/2005
1S        100     1/7/2005
1S        120     1/10/2005

As we can see for the one product, the amount 100 is same for all 3 dates. And also, 3 records are inserted.But instead of 3 records, if the amount of current row is same as previous row, then we need to just update DATE and no new records should be inserted.

If the current row contains a new amount 120 and not same as previous row, then a record should be inserted. Please see the below output that am expecting

Output:

 Product   -Amount         - Date

1S           100         1/7/2005   
1S           120         1/10/2005

Upvotes: 0

Views: 371

Answers (1)

praveen muppala
praveen muppala

Reputation: 177

You can do it as below. This would give the max date grouped by product and amount.This would be a simple query than merge

insert into tablea
(select product,amount,max(date) from tableb
group by product,amount).

Upvotes: 1

Related Questions