Finding unmatched data between two sql tables

I have two SQL 2000 table:
Table 1: UserID, Date
Table2: UserID, DateOut, DateIn

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.
sleimanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

franked_itCommented:
I'm not sure that I understand what you're trying to match as far as the date.  Does the UserID have to match AND the date needs to be between DateIn and DateOut?  Is this the case for both queries?

This will bring up all entries in Table 1 where the UserID is not in Table2:
SELECT * FROM table1 WHERE table1.UserID NOT IN (SELECT UserID FROM table2)

0
ee_rleeCommented:
to get all records which falls within the date:

SELECT * from table1, table2 where table1.userid=table2.userid and table1.date>=table2.datein and table1.date<=table2.dateout

to get records that is not within range:
SELECT * from table1, table2 where table1.userid=table2.userid and (table1.date<table2.datein or table1.date>table2.dateout)
0
TextReportCommented:
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

Cheers, Andrew
0
LowfatspreadCommented:
Hello sleiman,

you should use use a left outer join then...


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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sleimanAuthor Commented:
thanks all.

LowFatspread's solution was best to do what I need in one show.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.