Reputation: 865
I have two tables like so:
Table_1
ID_1 | VALUES_1
1 | 123
2 | 234
3 | 345
Table_2
ID_2 | VALUES_2
1 | 123
2 | 234
3 | 349
4 | 456
5 | 567
6 | 678
I'm doing an inner join to compare the values from both tables:
SELECT ID_1, VALUES_1, TABLE_2.VALUES_2
FROM TABLE_1,TABLE_2
WHERE
TABLE_1.ID_1 = TABLE_2.ID_2 AND
TABLE_1.VALUES_1 <> TABLE_2.VALUES_2
Which gives me the rows I am interested in,
ID_1 | VALUES_1 | VALUES_2
3 | 345 | 349
Is there a way to insert a third column as part of the query to display the difference between VALUES_1
and VALUES_2
? E.g.
ID_1 | VALUES_1 | VALUES_2 | DIFFERENCE
3 | 345 | 349 | 4
Upvotes: 0
Views: 150
Reputation: 14341
SELECT
ID_1
,VALUES_1
,TABLE_2.VALUES_2
,TABLE_2.VALUES_2 - VALUES_1 as Differnce
FROM
TABLE_1
INNER JOIN TABLE_2
ON TABLE_1.ID_1 = TABLE_2.ID_2
AND TABLE_1.VALUES_1 <> TABLE_2.VALUES_2
Note I also converted your query to use explicit join syntax instead of implicit join as that is the more standard method.
Upvotes: 1