Leagis
Leagis

Reputation: 175

Join two tables with the same composite key

I have to find the differences between two tables. Both have the same composite keys.

So Table A is:

Vendor Material    
    Vendor1 Mat1
    Vendor1 Mat2
    Vendor1 Mat3
    Vendor 2 Mat 4

And Table B is:

Vendor Material    
    Vendor1 Mat1
    Vendor1 Mat2
    Vendor1 Mat3
    Vendor1 Mat4
    Vendor2 Mat4

I need to figure out how to get the result Vendor1 Mat4.

I tried

Select TableB.Vendor, TableB.Material From TableB,TableA, where TableB.Vendor <> TableA.Vendor AND TableB.Material <> TableA.Material;

But this doesn't help me, because I get a lot more results than expected.

Upvotes: 0

Views: 57

Answers (2)

Dony George
Dony George

Reputation: 175

  SELECT TableB.Vendor , TableB.Material 
    FROM TableB LEFT JOIN TableA on TableB.Vendor =TableA.Vendor and TableB.Material = TableA.Material 
    WHERE tableA.vendor IS NULL

Upvotes: 0

Jens
Jens

Reputation: 69470

This query should give you the result:

Select TableB.Vendor TableB.Material 
from TableB join TableA on  TableB.Vendor =TableA.Vendor and TableB.Material = TableA.Material 
where tableA.vendor is null;

Upvotes: 2

Related Questions