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 |
|
krisdotca
Starting Member
6 Posts |
Posted - 2010-04-27 : 16:05:17
|
| Hi There,I have a view that joins a number of tables. Some of the join columns may have NULL values - e.g. assignedphysician.I read here http://www.mssqltips.com/tip.asp?tip=1447 that the way to allow NULL in a join is to use something like: isnull(a.Trim,'999999') = isnull(b.Trim,'999999'). Does anyone know of a more elegant way to do that - seems like a bit of a hack to use a string that you hope will not occur in your db. I've included my big ugly join below in case it helps. :)CREATE VIEW vw_CasesFacilitiesUsersAS SELECT cases.CaseId, cases.CreationDate, cases.CaseTitle, cases.WorkflowStep ,patient.UserId AS PatientUserId, patient.UserName AS PatientUserName ,assignedphysician.UserId AS AssignedPhysicianUserId, assignedphysician.UserName AS AssignedPhysicianUserName ,createdbyuser.UserId AS CreatedByUserId, createdbyuser.UserName AS CreatedByUserName ,createdbyfacility.FacilityId AS CreatedByFacilityId, createdbyfacility.FacilityName AS CreatedByFacilityName ,assignedfacility.FacilityId AS AssignedFacilityId, assignedfacility.FacilityName AS AssignedFacilityNameFROM Cases AS casesJOIN aspnet_Users AS patient ON cases.PatientUserId = patient.UserIdJOIN aspnet_Users AS assignedphysician ON cases.AssignedPhysicianUserId = assignedphysician.UserIdJOIN aspnet_Users AS createdbyuser ON cases.CreatedByUserId = createdbyuser.UserIdJOIN Facilities AS createdbyfacility ON cases.CreatedByFacilityId = createdbyfacility.FacilityIdJOIN Facilities AS assignedfacility ON cases.AssignedFacilityId = assignedfacility.FacilityIdGO |
|
|
krisdotca
Starting Member
6 Posts |
Posted - 2010-04-27 : 16:19:35
|
| I think I answered my own question but if someone could confirm for me, that would be great. I modified the join to specify exactly how the NULL columns will be handled using LEFT INNER JOIN as follows:---------------------------------------------------------------------- view between Cases, Facilities and aspnet_User ----------------------------------------------------------------------CREATE VIEW vw_CasesFacilitiesUsersAS SELECT cases.CaseId, cases.CreationDate, cases.CaseTitle, cases.WorkflowStep ,patient.UserId AS PatientUserId, patient.UserName AS PatientUserName ,assignedphysician.UserId AS AssignedPhysicianUserId, assignedphysician.UserName AS AssignedPhysicianUserName ,createdbyuser.UserId AS CreatedByUserId, createdbyuser.UserName AS CreatedByUserName ,createdbyfacility.FacilityId AS CreatedByFacilityId, createdbyfacility.FacilityName AS CreatedByFacilityName ,assignedfacility.FacilityId AS AssignedFacilityId, assignedfacility.FacilityName AS AssignedFacilityNameFROM Cases AS casesINNER JOIN aspnet_Users AS patient ON cases.PatientUserId = patient.UserIdLEFT OUTER JOIN aspnet_Users AS assignedphysician ON cases.AssignedPhysicianUserId = assignedphysician.UserIdINNER JOIN aspnet_Users AS createdbyuser ON cases.CreatedByUserId = createdbyuser.UserIdINNER JOIN Facilities AS createdbyfacility ON cases.CreatedByFacilityId = createdbyfacility.FacilityIdLEFT OUTER JOIN Facilities AS assignedfacility ON cases.AssignedFacilityId = assignedfacility.FacilityIdGO |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-27 : 16:20:57
|
quote: Originally posted by krisdotca Hi There,I have a view that joins a number of tables. Some of the join columns may have NULL values - e.g. assignedphysician.
If it's the "cases.AssignedPhysicianUserId" column that has the nulls, then you can use a LEFT JOIN instead of a JOIN. A LEFT JOIN returns all rows for the left table, regardless of whether or not there's a matching record in the right table. A JOIN (or INNER JOIN) only includes records from the left table that match records in the right table (and visa versa).------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2010-04-27 : 16:22:27
|
| Please illustrate relations and some examples. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|
|
|
|