Ranjith Reddy Ranjith Reddy - 1 year ago 34
SQL Question

Adding values from two table and storing into third table

INSERT INTO Table1 (Amount, ContactName, State, City)
values
(
(Select Amount from Table2 where id=12)+
(Select Amount from Table3 where id=13),
'Sam',Newyork,'Newyork'
);


I was trying to Store the value of amount in Table1 by the sum of Table2 + Table3
Amount

Answer Source

Try with nvl(max(Amount),0). The MAX will remove possible duplication and if there will be no rows you will get NULL. The NVL will remove possible NULL and it will replace it with 0.

INSERT INTO Table1 (Amount, ContactName, State, City) 
values 
(
    (Select nvl(max(Amount),0) from Table2 where id=12)+
    (Select nvl(max(Amount),0) from Table3 where id=13),
    'Sam','Newyork','Newyork'
);