Question : SQL Query that needs a helping hand

I'm hoping that someone can help me here. Below is a simple piece of code I have that I'm using to create a View from my MS CRM database. It basically looks up my Filtered Accounts View and gives me some details back. However, I need to check against the Filtered Contacts View to see if Address1_Line1 and Address1_Line2 from Filtered Accounts matches the PrimaryContactID's address details.

So The Filtered Contact View is called: FilteredContact

The 2 Fields I need to match against are:
FilteredAccount.Address1_Line1 against FilteredContact.Address1_Line1
FilteredAccount.Address1_Line2 against FilteredContact.Address1_Line2

And the FilteredAccount.PrimaryContactID = FilteredContact.ContactID

If the fields match then I'd like to have a new column in my SELECT statement below that has a field heading of AddressMatch and a 0 if it doesn't match and a 1 if it does.

Hope that makes sense any any help is appreciated as always.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
SELECT     TOP (100) PERCENT CONVERT(VARCHAR(36), accountid) AS Accountid, name AS BusinessName, address1_line1, address1_line2, address1_line3, address1_city, 
                      address1_county, address1_postalcode, emailaddress1, fax, new_aftercareflag, new_businessadvisorifpsybtidname, new_businessadvsoridname, 
                      new_businessdescription, new_numberoffulltimejobs, new_numberofparttimejobs, new_region, new_regionname, websiteurl, telephone1, statuscode, 
                      new_businessclassificationcode, new_businessclassificationcodename, new_businessstatus, new_businessstatusname
FROM         dbo.FilteredAccount
WHERE     (new_businessstatus = 2) AND (new_businessdirectory = 1) AND (statuscode = 1) AND (new_clientbusiness = 1) OR
                      (new_businessdirectory IS NULL) AND (statuscode = 1) AND (new_aftercareflag = 1) OR
                      (new_businessdirectory = 1) AND (statuscode = 1) AND (new_aftercareflag = 1)
ORDER BY LEN(new_businessdescription) DESC

Answer : SQL Query that needs a helping hand

you can use
case
   when condition
   then value
   when ...
    then ...
    else ..
end
Random Solutions  
 
programming4us programming4us