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
 General SQL Server Forums
 New to SQL Server Programming
 Join on a column with null values

Author  Topic 

sqlbug
Posting Yak Master

201 Posts

Posted - 2009-02-26 : 11:34:12
Hi,

I have a join on this column that can have null values in it. But I still want those records. Basically it is a self join, trying to find the parents. I tried like:

SELECT DISTINCT C.FACILITYSID, C.FacilityCode, C.NAME, FACTYPELABEL,PROVSTATELABEL,P.NAME
FROM FACILITY_INFO C,FACILITYTYPE_CODE,PROVSTATE_CODE,
FACILITY_INFO P
WHERE C.TYPEID = FACTYPESID AND C.PROVINCEID = PROVSTATESID
AND (C.ParentFacilityID = P.FACILITYSID OR C.ParentFacilityID = NULL)

But it still gives me the matching rows.
Any help?
Thanks.

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-26 : 11:44:41
Can you use Left Outer Join?
Go to Top of Page

sqlbug
Posting Yak Master

201 Posts

Posted - 2009-02-26 : 12:44:19
Can you show me please?
Thanks.
Go to Top of Page

SQLforGirls
Starting Member

48 Posts

Posted - 2009-02-26 : 12:57:55
This is how the left join would look

SELECT DISTINCT
C.FACILITYSID,
C.FacilityCode,
C.NAME,
FL.FACTYPELABEL,
PL.PROVSTATELABEL,
P.NAME
FROM FACILITY_INFO P
LEFT JOIN FACILITY_INFO C ON P.FACILITYSID = C.ParentFacilityID
JOIN FACILITYTYPE_CODE FC ON C.TYPEID = FC.FACTYPESID
JOIN PROVSTATE_CODE PC ON C.PROVINCEID = PC.PROVSTATESID

But depending on what you mean when you say there could be NULL records....

If this doesn't solve it for you exactly, please post some sample data showing where the NULL values might be.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-02-26 : 13:35:16
sqlbug, you can thank them in this topic. I nuked your new topic just saying "thanks".

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -