join based on values

Using SQL Server 2008 R2.

I want join table a to table b on a.businessKey = b.TMonths.

When b.TMonths >= 120 then it should join to businesskey  =  '>120' on table a;
when  b.TMonthsis null, then it should join to businesskey = '0' on table a;
In all other cases a.businessKey = b.TMonths

How to create this Join?
patd1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
Try
 
Select * from a
join b on a.businesskey='>120'
where b.TMonths>=120
UNION ALL
Select * from a
join b on a.businesskey='0'
where b.TMonths IS NULL
UNION ALL
Select * from a
join b on a.businessKey = b.TMonths
where (b.TMonths IS NOT NULL) AND b.TMonths<120

Open in new window

Jim HornMicrosoft SQL Server Data DudeCommented:
< stand back, haven't tried this before... >

SELECT blah, blah, blah
FROM a
   JOIN b ON CASE 
       WHEN ISNULL(b.Tmonth, 0) THEN '0'
       WHEN b.TMonths >= 120 THEN '>120'
       ELSE CAST(b.TMonths as varchar(?)) = a.businesskey

Open in new window

Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
Or try:
 
Select * from
(Select *, case when TMonths>120 then '>120' when TMonths is null then '0' else convert(varchar(20),TMonths) end as BusinessKeyLink
From b) as c
join a when c.BusinessKeyLink = a.businessKey

Open in new window

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
Vitor MontalvãoMSSQL Senior EngineerCommented:
The ISNULL function will transform the NULL values in zero and the UNION (not UNION ALL) will take care of the rest of the rows that the first SELECT filtered (a.businesskey<120):
SELECT A.* 
FROM a
INNER JOIN b ON a.businessKey = ISNULL(b.TMonths,0)
WHERE b.TMonths<120
UNION 
SELECT a.* 
FROM a

Open in new window

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.