BobSki
BobSki

Reputation: 1552

Join two tables and display records only if 2nd table does not have a matching date

I have two tables that I'm trying to join. I want to be able to join on ClientID, and only display those records where DateA does not match DateB. This is the structure of my tables

tbl1
CLientID             DateA
17                   08/01/2016
19                   06/27/2016
21                   12/1/2015


tbl2
RcrNo            ClientID           DateB
1                17                 08/01/2016
2                19                 06/27/2016
3                21                 12/22/2015

Desired Result:

ClientID             Date
21                   12/22/2015

SO as can be seen ClientID 21 is the only client out of the 3 whose DateA does not match DateB. How would I be able to join them like this?

Upvotes: 0

Views: 35

Answers (2)

AB_87
AB_87

Reputation: 1156

Using JOIN and WHERE clause

SELECT *
FROM   table1
       JOIN table2 ON table1.ClientId = table2.ClientId
WHERE  table1.date <> table2.date;

Upvotes: 2

Gordon Linoff
Gordon Linoff

Reputation: 1270031

You can use except:

select clientid, dateb
from tbl2
except
select clientid, datea
from tbl1;

Upvotes: 1

Related Questions