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.
| Author |
Topic |
|
wink321
Starting Member
2 Posts |
Posted - 2009-08-31 : 20:53:50
|
| Hi,I am trying to join 3 tables (Employee, EmployeeOrg, and Org) structured below. I need to return all Org names no matter the membership, so an outer join definitely should apply here, but it's not working.The dev environment uses SQL Server Express 2005, but Production will be SQL Server 2005. The query is:SELECT E.EmpName, A.OrgName, EA.OrgIDFROM EmployeeOrg EAINNER JOIN Employee AS E ON EA.EmpID = E.EmpIDLEFT OUTER JOIN Org AS A ON EA.OrgID = A.OrgID Data: EmpID EmpName -------------- 1001 M. Scott 1002 P. Beesly 1003 D. Schrute EmpID OrgID------------------1001 11001 31002 21003 1OrgID OrgName------------------1 PTA2 IEEE3 AAADesired output: EmpName OrgName OrgID----------------------------------------M. Scott PTA 1M. Scott IEEE NULL M. Scott AAA 3P. Beesly PTA NULLP. Beesly IEEE 2P. Beesly AAA NULLD. Schrute PTA 1D. Schrute IEEE NULLD. Schrute AAA NULLIt seems to be treating the left outer join as a regular join (not returning any NULLs). If I just join the OrgName and OrgID tables I do get some nulls, but I really need the additional join to the Emp table. I've read many examples and articles online and this should be working but maybe I'm missing something on the syntax. Any help or pointers would be appreciated! |
|
|
wink321
Starting Member
2 Posts |
Posted - 2009-08-31 : 21:09:48
|
| Rudy of r937.com replied on another forum with the following answer, which worked!SELECT E.EmpName , A.OrgName , EA.OrgID FROM Employee AS E CROSS JOIN Org AS A LEFT OUTER JOIN EmployeeOrg EA ON EA.EmpID = E.EmpID AND EA.OrgID = A.OrgID |
 |
|
|
|
|
|