Skip to content
Advertisement

How to insert a Number value which is a difference between field 1 and field 2 of another table in Oracle?

I have a table name ITEM that consist of

+-------+-----------+--------------+
| Item  | PrevValue | CurrentValue |
+-------+-----------+--------------+
| ItemA |         2 |        10    |
| ItemB |         1 |      2       |
+-------+-----------+--------------+

And another table name PAYMENT that will require insert.

    INSERT INTO PAYMENT(ID, Billing_Date, Due_Date, Value_Amount)
    VALUES (1,TO_DATE('23-JULY-2020','DD-MON-YYYY'), TO_DATE('21-AUGUST-2020','DD-MON-YYYY'),
**CurValue - PrevValue** );

How do i correctly calculate the differences between PrevValue and CurValue in ITEM and insert them into PAYMENT?

Advertisement

Answer

The query will be, simply INSERT….SELECT:

INSERT INTO PAYMENT(ID, Billing_Date, Due_Date, Value_Amount)
    select 1,TO_DATE('23-JULY-2020','DD-MON-YYYY'), TO_DATE('21-AUGUST-2020','DD-MON-YYYY'), (CurValue - PrevValue) from ITEM where Item = '<Item_Name>';
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement