Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with Join expression needed

Author  Topic 

barnabeck
Posting Yak Master

236 Posts

Posted - 2010-06-17 : 07:12:01
I tried everything I know, but the tables won't join the way I want them to join:

Table_A Table_B
-------- --------
DATE CALENDAR
18.6.2010 17.6.2010
18.6.2010 18.6.2010
19.6.2010 19.6.2010
21.6.2010 20.6.2010
21.6.2010 21.6.2010

I want ALL records from Table_A plus those records from Table_B with Calendar-dates that haven't any match in Table_A.(17.6.2010 & 20.6.2010)

Select CASE WHEN Table_A.DATE IS NULL THEN Table_B.CALENDAR ELSE Table_A.DATE AS ALLDATE

The WHERE CLAUSE specifies those records in a 2 weeks period.

WHERE ALLDATE BETWEEN GETDATE() AND GETDATE()+13

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-06-17 : 07:28:24
select * from
(
select Date as ALLDATE from Table_A
union all
select CALENDAR from Table_B where not exists(select * from Table_A where Table_A.Date = Table_B.CALENDAR)
)dt
WHERE ALLDATE BETWEEN GETDATE() AND GETDATE()+13


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

barnabeck
Posting Yak Master

236 Posts

Posted - 2010-06-18 : 06:22:11
Thanks!!!
Helped a lot and taught even more as I adapted it to my query!
Martin
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-06-18 : 09:57:55
welcome


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -