Dilip
Dilip

Reputation: 155

I need some help in creating a SQL to find the difference between two tables and if there is a difference by how much they differ

I have two tables. A and B.

Table Structure:

A (A_ID, A_AMT, A_QTY)

B (B_ID, B_AMT, B_QTY)

I need to find IDs not with the same AMT and QTY values.

For example: If A_AMT is 5 and B_AMT is 10 then I need to find the difference between them as 5 and return them.

Can someone help me with this.

Note: Both the tables have the same IDs. A_ID = B_ID.

Upvotes: 0

Views: 31

Answers (1)

Fuzzy
Fuzzy

Reputation: 3810

Try this:

SELECT A.A_AMT,
       B.B_AMT, 
       A.A_AMT - B.B_AMT AS DIFF_AMT, 
       A.A_QTY, 
       B.B_QTY, 
       A.A_QTY - B.B_QTY AS DIFF_QTY
FROM       A INNER JOIN 
       B ON A.A_ID = B.B_ID
WHERE   A.A_AMT <> B.B_AMT OR A.A_QTY <> B.B_QTY

Upvotes: 2

Related Questions