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 |
jonclayIRM
Starting Member
12 Posts |
Posted - 2015-03-23 : 08:49:41
|
Hi thereI have a problem trying to get the following code to work correctly:SELECT DISTINCT Contacts.ContactID, Contacts.CategoryCode, Addresses.Country, Addresses.DateCreated, Attributes.AttributeCodeFROM dbo.ContactsFULL OUTER JOIN dbo.Addresses on Contacts.ContactID=Addresses.ContactIDFULL OUTER JOIN dbo.Attributes ON Contacts.ContactID=Attributes.ContactIDWHERE Contacts.CategoryCode IN ('SCT','SDP') and Deleted =0 ANDAddresses.DateCreated >='01/10/14' AND Addresses.DateCreated <='01/02/15' ANDAttributes.AttributeTypeID=29ORDER BY Contacts.ContactIDMy issue is that I want to show Attributes.AttributeCode where Attributes.AttributeType=29 - pretty easy and that's what I'm doing. However, there is also the potential for Attributes.AttributeType=29 to not be there at all, and I also want to show these records too. So, basically show all records as specified with Attributes.AttributeType=29 or where Attributes.AttributeType is missing.I hope this makes sense! Many thanks for your help. |
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2015-03-23 : 08:54:30
|
If there is no match on the Attributes table, then because you are doing a full outer join, the Attributes.AttributeTypeID column will be NULL,so you can extend your WHEREWHERE ...Attributes.AttributeTypeID=29 or Attributes.AttributeTypeID is NULL |
|
|
jonclayIRM
Starting Member
12 Posts |
Posted - 2015-03-23 : 08:56:35
|
Excellent, thank you! I hoped it would be as simple as that! |
|
|
|
|
|