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 |
Jon_Clay
Starting Member
3 Posts |
Posted - 2014-11-07 : 09:11:04
|
Hi there,I am trying to create a report that pulls out data that isn't there (I know this sounds silly!). I'll explain...I need to show a MAIN contact address for people, and an ALTERNATE address if it is present. Some of our customers have ALTERNATE addresses and some don't. The code below is only pulling out customers that have both a MAIN and an ALTERNATE address.Incidentally, I'm trying to create this report in Crystal Reports, but this is the SQL that it's using:SELECT "Contacts"."ContactID", "Contacts"."Deleted", "SubscriptionMembers"."Lapsed", "SubscriptionMembers"."MainSubscription", "SubscriptionMembers"."Current", "Categories"."CategoryType", "Addresses"."CompanyName", "Addresses"."AddressType", "Addresses"."Address1", "Addresses"."Address2", "Addresses"."Address3", "Addresses"."Town", "Addresses"."County", "Addresses"."PostCode", "Addresses"."Country", "Addresses"."Telephone1", "Addresses"."Telephone2", "Addresses"."Fax", "Contacts"."MobileTelephone", "Contacts"."EMail", "Contacts"."WebSite", "Categories"."CategoryName", "Contacts"."Title", "Contacts"."Forenames", "Contacts"."Surname", "Contacts"."DateOfBirth", "Addresses2"."CompanyName", "Addresses2"."Address1", "Addresses2"."Address2", "Addresses2"."Address3", "Addresses2"."Town", "Addresses2"."County", "Addresses2"."PostCode", "Addresses2"."Country", "Addresses2"."Telephone1", "Addresses2"."Telephone2", "Addresses2"."Fax", "Addresses2"."AddressEmail", "Contacts"."Position", "Addresses"."Department", "Addresses2"."AddressType", "Addresses2"."Position", "Addresses2"."Department" FROM ((("Contacts" "Contacts" INNER JOIN "SubscriptionMembers" "SubscriptionMembers" ON "Contacts"."ContactID"="SubscriptionMembers"."ContactID") FULL OUTER JOIN "Addresses" "Addresses" ON "Contacts"."ContactID"="Addresses"."ContactID") LEFT OUTER JOIN "Categories" "Categories" ON "Contacts"."CategoryCode"="Categories"."CategoryCode") FULL OUTER JOIN "EnterpriseMRM"."dbo"."Addresses" "Addresses2" ON "Contacts"."ContactID"="Addresses2"."ContactID" WHERE "SubscriptionMembers"."Current"=1 AND "Categories"."CategoryType"=N'M' AND "SubscriptionMembers"."MainSubscription"=1 AND "Addresses"."AddressType"=N'MAIN' AND "Addresses2"."AddressType"=N'ALTERNATE' AND "Addresses"."Country"=N'United Kingdom' AND "SubscriptionMembers"."Lapsed"=0 AND "Contacts"."Deleted"=0 ORDER BY "Contacts"."ContactID", "Categories"."CategoryName"Any help would be much appreciated!Many thanks for your time,Jon |
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2014-11-07 : 10:00:23
|
You will be a lot better off if you write the query yourself rather than try to get some design tool to generate it.Start with something like:SELECT C.ContactID, C.Deleted, M.Lapsed, M.MainSubscription, M.[Current], K.CategoryType ,A.CompanyName, A.AddressType, A.Address1, A.Address2, A.Address3, A.Town, A.County, A.PostCode, A.Country, A.Telephone1, A.Telephone2, A.Fax ,C.MobileTelephone, C.EMail, C.WebSite, Categories.CategoryName, C.Title, C.Forenames, C.Surname, C.DateOfBirth ,A2.CompanyName, A2.Address1, A2.Address2, A2.Address3, A2.Town, A2.County, A2.PostCode, A2.Country, A2.Telephone1, A2.Telephone2, A2.Fax, A2.AddressEmail ,C.Position, A.Department, A2.AddressType, A2.Position, A2.DepartmentFROM Contacts C JOIN SubscriptionMembers M ON C.ContactID = M.ContactID JOIN Categories K ON C.CategoryCode = K.CategoryCode LEFT JOIN Addresses A ON C.ContactID = A.ContactID -- If these conditions are in the WHERE then the LEFT JOIN will be converted to an INNER JOIN AND A.AddressType = N'MAIN' AND A.Country = N'United Kingdom' LEFT JOIN EnterpriseMRM.dbo.Addresses A2 ON C.ContactID = A2.ContactID -- If this condition is in the WHERE then thn the LEFT JOIN will be converted to an INNER JOIN AND A2.AddressType=N'ALTERNATE'WHERE S.[Current] = 1 AND K.CategoryType = N'M' AND M.MainSubscription = 1 AND M.Lapsed = 0 AND C.Deleted=0ORDER BY C.ContactID, K.CategoryName |
|
|
Jon_Clay
Starting Member
3 Posts |
Posted - 2014-11-07 : 10:27:18
|
Thank you Ifor.I will take a look through the code and see what I can do. I am using Crystal Reports because the intended report is quite visual (it is to be sent out to our customers), and I'm unsure how to make it look pretty just by using SQL in raw form!Best wishesJon |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2014-11-07 : 11:14:02
|
Use Crystal Reports to design the report. Do not use it to write SQL. |
|
|
Jon_Clay
Starting Member
3 Posts |
Posted - 2014-11-07 : 11:28:34
|
Ah yes, I am using it to design the report. The SQL was produced by Crystal Reports automatically. |
|
|
|
|
|
|
|