Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
I apologize if this is a dumb question, but I am new at this and need some help...I have 3 tables with the following fieldsEmpTable: ENAMEOccTable: ENAME, RMIDRoomTable: RMID The EmpTable has all employees. The OccTable has only employees that are assigned a room. The RoomTable has all rooms. I am looking for all employees and all rooms. My expected results would look like:ENAME - RMIDJohn Doe - RoomABCnull - RoomXYZJane Doe - nullI keep getting all rooms or all employees. I can't seem to get both. Thanks in advance for your patience.
djj55
Constraint Violating Yak Guru
352 Posts
Posted - 2014-05-16 : 14:19:41
What does your query look like? I believe you need a full join.djj
SQLNewbieHelp
Starting Member
3 Posts
Posted - 2014-05-16 : 14:29:07
I've tried: SELECT EmpTable.ENAME, RoomTable.RMIDFROM EmpTable full OUTER JOIN OccTable ON EmpTable.ENAME = OccTable.ENAME FULL OUTER JOIN RoomTable ON OccTable.RMID = RoomTable.RMIDI've also tried right outer joins.
Lamprey
Master Smack Fu Yak Hacker
4614 Posts
Posted - 2014-05-16 : 14:38:44
Here is one way
DECLARE @Emp TABLE (ENAME VARCHAR(10))DECLARE @Occ TABLE (ENAME VARCHAR(10), RMID VARCHAR(10))DECLARE @Room TABLE (RMID VARCHAR(10))INSERT @Emp VALUES('John'), ('Jane')INSERT @Occ VALUES('John', 'Room1')INSERT @Room VALUES('Room1'), ('Room2')SELECT Emp.ENAME ,Room.RMIDFROM @Emp AS EmpLEFT OUTER JOIN @Occ AS Occ ON Emp.ENAME = Occ.ENAMEFULL OUTER JOIN @Room AS Room ON Occ.RMID = Room.RMID
SQLNewbieHelp
Starting Member
3 Posts
Posted - 2014-05-16 : 15:30:07
I tried that too. It's giving me the rooms with the assigned employees, but not the employees that do not have an assigned room. In my example, it's not giving me the Jane Doe - null.