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 |
|
bbowser
Starting Member
43 Posts |
Posted - 2009-10-12 : 09:42:08
|
| I have a tblContacts that I have a Primary Key on ContactID (int) and I'm associating other contacts from the same table with other INT columns. I need to compare the tblContacts.ContactID with the ManagementCoID (INT) column and return the BusinessName. Here is my query. Please help, I'm at my wits end.SELECT tblContacts.BusinessName, tblContacts.Phone, tblContacts.City, tblContacts.State, (SELECT BusinessName FROM tblContacts WHERE (ContactID = ManagementCoID)) AS ManagementCoFROM tblContacts Inner Join tblContactsToRating ONtblContacts.ContactID = tblContactsToRating.ContactIDWHERE (tblContactsToRating.RatingsID = '33') OR (tblContactsToRating.RatingsID = '38') |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2009-10-12 : 09:46:30
|
I guess ManagementCoID is from tblContacts. If the following is incorrect, please post some sample data and your expected result set.SELECT c.BusinessName, c.Phone, c.City, c.State, case when c.ContactID = ManagementCoID then BusinessName end as BusinessNameFROM tblContacts cInner Join tblContactsToRating cr ON c.ContactID = cr.ContactIDWHERE (cr.RatingsID = '33') OR (cr.RatingsID = '38') |
 |
|
|
bbowser
Starting Member
43 Posts |
Posted - 2009-10-12 : 10:17:13
|
| Yes, ManagementCoID is from the tblContacts table. I tried your solution and it didn't return the BusinessName It just returned NULL even though I have the INT value for the ManagementCOID in the table. Let's say my table is like thisContactID BusinessName Address1 ManagementCoID2212 Diamond Minds 123 Main Street 42184218 Lakeland, Inc 419 S. Haven NULLI want to return:BusinessName Address1 BusinessName2Diamond Minds 123 Main Street Lakeland, Inc. |
 |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2009-10-12 : 11:48:20
|
Ok, thats better, try this instead then:SELECT c.BusinessName, c.Phone, c.City, c.State, t2.BusinessNameFROM tblContacts cInner Join tblContactsToRating cr ON c.ContactID = cr.ContactIDleft join (select BusinessName, ManagementCoID from tblContacts) t2ON t2.ManagementCoID = c.ContactIDWHERE (cr.RatingsID = '33') OR (cr.RatingsID = '38') |
 |
|
|
bbowser
Starting Member
43 Posts |
Posted - 2009-10-12 : 12:41:15
|
| Okay, I tried that solution and it didn't work either. I'm posting the entire query so you can see that if it helps you. The results that came back for the t2.BusinessName AS ManagementCo was NULL and I've triple checked that I do have an INT number for the ManagementCoID in several records. I'm using Visual Studio 2005 for a SQL report if that makes any difference. Thank you for all your help.SELECT tblContacts_1.BusinessName, tblContacts_1.Phone, tblContacts_1.City, tblContacts_1.State, tblRatings.RatingName, tblActionPlan.ActionPlanNote, tblActionPlan.UserID, tblOutcome.OutcomeNote, tblActionPlan.ActionPlanUpdated, tblPropertyEvent.PropertyEventDivision, tblPropertyEvent.PropertyEventName, t2.BusinessName AS ManagementCoFROM tblOutcome RIGHT OUTER JOIN tblContacts AS tblContacts_1 INNER JOIN tblContactsToRating ON tblContacts_1.ContactID = tblContactsToRating.ContactID INNER JOIN tblRatings ON tblContactsToRating.RatingsID = tblRatings.RatingsID INNER JOIN tblActionPlan INNER JOIN tblPropertyEvent ON tblActionPlan.PropertyEventID = tblPropertyEvent.PropertyEventID ON tblContacts_1.ContactID = tblPropertyEvent.ContactID ON tblOutcome.ActionPlanID = tblActionPlan.ActionPlanID LEFT OUTER JOIN (SELECT BusinessName, ManagementCoID FROM tblContacts) AS t2 ON t2.ManagementCoID = tblContacts_1.ContactIDWHERE (tblContactsToRating.RatingsID = '33') OR (tblContactsToRating.RatingsID = '38')**** I hope this helps **** |
 |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2009-10-13 : 06:05:35
|
From the data you gave me, the first query works, without more sample data, I can't tell you what is wrong with your query.This works:declare @tblContacts as table (ContactID int, BusinessName varchar(100), Address1 varchar(100), ManagementCoID int)insert into @tblContactsselect 2212, 'Diamond Minds', '123 Main Street', 4218union select 4218, 'Lakeland, Inc', '419 S. Haven', NULLSELECT c.BusinessName, c.Address1, t2.BusinessNameFROM @tblContacts cleft join (select BusinessName, ManagementCoID from @tblContacts) t2ON t2.ManagementCoID = c.ContactID So I woould suggest it was a data issue with one of the other tables in your query. |
 |
|
|
|
|
|
|
|