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 2008 Forums
 Transact-SQL (2008)
 Select Statment Question

Author  Topic 

BradleyGZ
Starting Member

4 Posts

Posted - 2011-02-04 : 11:26:37
I am joining 4 tables. 3 of these tables will always have linking data. (DatEmployee will always have a linked record in RefDepartment and RefTitle) However, there will not always be a record in DatAlias. If there is then I want that value but only is a bit field is set to true. Here is my statement.

SELECT dbo.DatEmployee.*, dbo.RefDepartment.DeptName, dbo.RefDepartment.DeptNum, dbo.RefTitle.JobTitle, dbo.RefTitle.WorkCompRef, dbo.DatAlias.Alias
FROM dbo.DatEmployee INNER JOIN
dbo.RefDepartment ON dbo.DatEmployee.HomeDeptRef = dbo.RefDepartment.DeptRef INNER JOIN
dbo.RefTitle ON dbo.DatEmployee.HomeTitleRef = dbo.RefTitle.TitleRef LEFT OUTER JOIN
dbo.DatAlias ON dbo.DatEmployee.TinNum = dbo.DatAlias.TinNum
WHERE (dbo.DatAlias.IsSSAName = 1)

This wont return anything if there is not a record in DatAlias with IsSSAName = True.

Thanks in advanced.
Brad.

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-02-04 : 11:30:21
Change the WHERE to AND se if that works

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

BradleyGZ
Starting Member

4 Posts

Posted - 2011-02-04 : 11:35:37
Thanks for the quick response Jim.
That worked. I never would have figured that simple one out.

Thanks again!
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-02-04 : 11:45:16
You're welcome. You can Google Join vs Where and find some good articles on why this worked.

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -