*RESOLVED* Rec set f/ Report from multiple tables
I am still searching for the answer to this question (have tried SQL forums elsewhere, but no one is responding 'yay' or 'nay')
What I need is - given a company ID, find all the records tied to that company across 4 tables. That much I can do.
The problem is I need to take a piece of that result (a contact name) and look up their address in same table the company's address came out of.
I have tried referencing the table twice, once by name and once by alias and use the alias for the contact info.
And that NEARLY works! Except it returns multiple records (it should return a single record with two distinct addresses within that record). What I actually get is the company info, complete with company address, repeated 6 times with 6 different contact names and their subsequent addresses!!!
It is returning the correct company based on the ID passed to the query, but then returns EVERY contact in the sample database, rather than the single contact which is tied to that company.
It looks messy, but here is the query string:
Code:
SELECT tblCorp.*, tblOrganization.*, tblStateReports.*,
tblAddressBook.Address1Mailing,
tblAddressBook.Address1Main,
tblAddressBook.Address2Mailing,
tblAddressBook.Address2Main, tblAddressBook.Attorney,
tblAddressBook.CityMailing, tblAddressBook.CityMain,
tblAddressBook.EmailAddress, tblAddressBook.FAXNumber,
tblAddressBook.FullName, tblAddressBook.ID,
tblAddressBook.PrimaryPhone,
tblAddressBook.RegisteringAgent,
tblAddressBook.SecondaryPhone,
tblAddressBook.StateMailing, tblAddressBook.StateMain,
tblAddressBook.ZIPMailing, tblAddressBook.ZIPMain,
RegAddress.FullName AS RegAgent,
RegAddRess.Address1Reg, RegAddress.Address2Reg,
RegAddress.CityReg, RegAddress.StateReg,
RegAddress.ZIPReg
FROM tblAddressBook, tblCorp, tblOrganization, tblStateReports,
tblAddressBook AS RegAddress
WHERE (tblAddressBook.ID = tblCorp.ID AND
tblCorp.ID = tblOrganization.ForeignKey AND
tblOrganization.ID = tblStateReports.ForeignKey AND
tblCorp.ID = ? AND tblOrganization.OrganizedAuthorized = 'O')
AND (tblCorp.ID = ? AND RegAgent = tblCorp.RegisteredAgent)
HELP! I need to deliver this report TOMORROW and I can't make the stinking thing work :(
Re: Rec set f/ Report from multiple tables
Quote:
Originally posted by mdsoren
I am still searching for the answer to this question (have tried SQL forums elsewhere, but no one is responding 'yay' or 'nay')
What I need is - given a company ID, find all the records tied to that company across 4 tables. That much I can do.
The problem is I need to take a piece of that result (a contact name) and look up their address in same table the company's address came out of.
I have tried referencing the table twice, once by name and once by alias and use the alias for the contact info.
And that NEARLY works! Except it returns multiple records (it should return a single record with two distinct addresses within that record). What I actually get is the company info, complete with company address, repeated 6 times with 6 different contact names and their subsequent addresses!!!
It is returning the correct company based on the ID passed to the query, but then returns EVERY contact in the sample database, rather than the single contact which is tied to that company.
It looks messy, but here is the query string:
Code:
SELECT tblCorp.*, tblOrganization.*, tblStateReports.*,
tblAddressBook.Address1Mailing,
tblAddressBook.Address1Main,
tblAddressBook.Address2Mailing,
tblAddressBook.Address2Main, tblAddressBook.Attorney,
tblAddressBook.CityMailing, tblAddressBook.CityMain,
tblAddressBook.EmailAddress, tblAddressBook.FAXNumber,
tblAddressBook.FullName, tblAddressBook.ID,
tblAddressBook.PrimaryPhone,
tblAddressBook.RegisteringAgent,
tblAddressBook.SecondaryPhone,
tblAddressBook.StateMailing, tblAddressBook.StateMain,
tblAddressBook.ZIPMailing, tblAddressBook.ZIPMain,
RegAddress.FullName AS RegAgent,
RegAddRess.Address1Reg, RegAddress.Address2Reg,
RegAddress.CityReg, RegAddress.StateReg,
RegAddress.ZIPReg
FROM tblAddressBook, tblCorp, tblOrganization, tblStateReports,
tblAddressBook AS RegAddress
WHERE (tblAddressBook.ID = tblCorp.ID AND
tblCorp.ID = tblOrganization.ForeignKey AND
tblOrganization.ID = tblStateReports.ForeignKey AND
tblCorp.ID = ? AND tblOrganization.OrganizedAuthorized = 'O')
AND (tblCorp.ID = ? AND RegAgent = tblCorp.RegisteredAgent)
HELP! I need to deliver this report TOMORROW and I can't make the stinking thing work :(
Perhaps a subquery? Something like this:
Declare SelectName varchar(500)
Set SelectName = (Select tblCorp.RegisteredAgent From tblCorp.RegisteredAgent Where tblCorp.ID = ?)
SELECT tblCorp.*, tblOrganization.*, tblStateReports.*,
...
FullName AS (Select Fullname From tblAddressBook Where tblAddressBook.ID = ? AND tblAddressBook.Fullname = SelectName)
Address1Reg AS (Select Address1Main From tblAddressBook Where tblAddressBook.ID = ? AND tblAddressBook.Fullname = SelectName)
...
FROM tblAddressBook, tblCorp, tblOrganization, tblStateReports,
...
Try that out. It's not optimal for speed, but it should get you what you need.