Finding unmatched data between two sql tables

UserID is used as a join between both tables
table1.Date needs to be matched as a date that falls between table2.Dateout and table2.datein
I need a sql code to first find recods that match in table2 and a seperate code to show all records from table1 that do not have a match in table2.

IN SQL Server you can use the INNER and OUTER joins to achieve what you are after.

"I need a sql code to first find recods that match in table2"
SELECT A.UserID, A.[Date]
FROM [Table 1] A
INNER JOIN [Table 2] B
ON A.Date BETWEEN DateOut AND DateIn

"seperate code to show all records from table1 that do not have a match in table2"
SELECT A.UserID, A.[Date]
FROM [Table 1] A
LEFT OUTER JOIN [Table 2] B
ON A.Date BETWEEN DateOut AND DateIn
WHERE B.UserID IS NULL

select Distinct a.*
,case when b.userid is null then 'Unmatched' else 'Matched' end as [Status]
from table1 as a
left outer join table2 as b
pn a.userid=b.userid
and a.date between b.dateout and b.datein
order by a.userid

Regards,

Lowfatspread

0

sleimanAuthor Commented: 2008-01-27

thanks all.

LowFatspread's solution was best to do what I need in one show.

0

Featured Post

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.