Reputation: 157
I have two tables has below
Table Left:
ID | Visit No
--- | --------
1 | 1
1 | 2
2 | 1
3 | 1
If Table Right has
ID | Visit No | Place
--- | -------- | -----
1 | 1 | Chennai
Then Results will be like
ID | Visit No
--- | --------
1 | 2
2 | 1
3 | 1
If Table Right is Empty, Then the Result will be
ID | Visit No
--- | --------
1 | 1
1 | 2
2 | 1
3 | 1
Based on ID and Visit No. Is it possible to get output using joins in SQL Sever 2012.
Upvotes: 0
Views: 39
Reputation: 564
SELECT left.[ID], left.[Visit No]
FROM LeftTable left
LEFT JOIN RightTable right
ON left.[ID] = right.[ID]
AND left.[Visit No] = right.[Visit No]
WHERE right.[ID] IS NULL
Upvotes: 1