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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 sql join using case statement??

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-03-27 : 09:10:17
Dev writes "I am joining a table in my query and depending on if another vcalue from a previous join is blank, I need to either join on one value or multiple values. How can this be done???

[select a.1, a.2, b.1, b.2, c.1, c.2
from a
left join b on a.1 = b.1 and a.2 = c.2]

(note: if c.2 is blank nothing comes back, but a record does exist that I need. If I take off the "and a.2 = c.2" I get my record)

Here is what I was thinking. Just dont know the syntax or if it is even possible.

[select a.1, a.2, b.1, b.2, c.1, c.2
from a
left join b on a.1 =
CASE when c.2 is null then b.1
ELSE b.1 and a.2 = c.2
END]


Note: I have used cases like this before and they work great, but only with one value following the "THEN" or "ELSE". It doesnt work with many values such as "b.1 and a.2 = c.2""

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-03-27 : 10:22:40
You are always better of with multiple LEFT OUTER JOINing to multliple tables (or aliases of the same table) on the different conditions, and using a CASE to decide which table to return values from.
Go to Top of Page
   

- Advertisement -