I think you need a FULL JOIN
(unless you actually want the row inserting to the first table)
SELECT COALESCE(t1.Seller, t2.Seller) AS Seller,
COALESCE(t1.Code, t2.Code) AS Code,
COALESCE(t1.Amount, 0) - COALESCE(t2.Amount, 0) AS Amount
FROM Table1 t1
FULL JOIN Table2 t2
ON t1.Seller = t2.Seller
AND COALESCE(t1.Code, 0) = COALESCE(t2.Code, 0);
If you do need the row inserting to table 1 you will need to do 2 operations, first insert, then select:
INSERT Table1 (Seller, Code, Amount)
SELECT t2.Seller, t2.Code, 0 AS Amount
FROM Table2 t2
WHERE NOT EXISTS
( SELECT 1
FROM Table1 t1
WHERE t1.Seller = t2.Seller
AND COALESCE(t1.Code, 0) = COALESCE(t2.Code, 0)
);
SELECT t1.Seller,
t1.Code,
t1.Amount - COALESCE(t2.Amount, 0) AS Amount
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.Seller = t2.Seller
AND COALESCE(t1.Code, 0) = COALESCE(t2.Code, 0);
EDIT
IF rows in each table are not unqiue and you need to sum them then you will need to do the sums in subqueries as the JOIN will introduce cross joining:
Consider this data
Table1
Seller Code Amount
VL 500 10
VL 500 20
Table2
Seller Code Amount
VL 500 30
VL 500 5
When you join this you will get:
t1.Seller t1.Code t1.Amount t2.Seller t2.Code t2.Amount
VL 500 10 VL 500 30
VL 500 10 VL 500 5
VL 500 20 VL 500 30
VL 500 20 VL 500 5
The sum of the difference is then -10 instead of -5.
SELECT COALESCE(t1.Seller, t2.Seller) AS Seller,
COALESCE(t1.Code, t2.Code) AS Code,
COALESCE(t1.Amount, 0) - COALESCE(t2.Amount, 0) AS Amount
FROM ( SELECT Seller, Code, SUM(Amount) AS Amount
FROM Table1
GROUP BY Seller, Code
) t1
FULL JOIN
( SELECT Seller, Code, SUM(Amount) AS Amount
FROM Table2
GROUP BY Seller, Code
) t2
ON t1.Seller = t2.Seller
AND COALESCE(t1.Code, 0) = COALESCE(t2.Code, 0);
EDIT 2
The UNION
method in Daniel's answer will perform much better than the FULL JOIN:
SELECT Seller, Code, Amount = SUM(Amount)
FROM ( SELECT Seller, Code, Amount
FROM Table1
UNION
SELECT Seller, Code, -Amount
FROM Table2
) t
GROUP BY Seller, Code