I need to perform a JOIN on a JOIN-ed table, and I’m not sure how to accomplish it. Hopefully, the query below demonstrates what I’m trying to do: get country names, each country’s leader, and each leader’s home town.
I think the problem with this query is using the JOIN-ed table “President” in the second join with President.HomeTown_Id. I don’t know what else to try.
SELECT
Countries.Name AS Country,
President.Name AS Leader,
PresidentHomeTown.Name AS LeaderHomeTown
FROM Countries
LEFT OUTER JOIN PoliticalFigures AS President ON Countries.President_Id = President.Id
LEFT OUTER JOIN Cities AS PresidentHomeTown ON President.HomeTown_Id = PresidentHomeTown.Id
In VS, I’m getting the error, “The multi-part identifier “President.Id” could not be bound.”
The names of tables and fields are fictitious, but I need to solve an identical problem. I changed the names to make things clearer; hopefully this will be relevant to more people.
— update —
Maybe the original code helps:
SELECT
CaseComparisons.Directory AS CaseComparisonDir,
BaselineResult.Directory AS BaselineResultDir,
ComparisonResult.Directory AS ComparisonResultDir,
Setup.FullSvnLink AS SvnLink,
BaselineVersion.FullFilePath AS BaselineExecutableDir
FROM CaseComparisons
LEFT OUTER JOIN Results AS BaselineResult ON CaseComparisons.BaselineResult_Id = Baseline.Id
LEFT OUTER JOIN Results AS ComparisonResult ON CaseComparisons.ComparisonResult_Id = Comparison.Id
LEFT OUTER JOIN Setups AS Setup ON Baseline.Setup_Id = Setups.Id
LEFT OUTER JOIN BuildVersions AS BaselineVersion ON BaselineResult.Version_Id = BuildVersions.Id
WHERE
CaseComparisons.Status = 'Queued' OR
Baseline.Status = 'Queued' OR
Comparison.Status = 'Queued'
The errors I get when I run the query:
The multi-part identifier "Baseline.Id" could not be bound.
The multi-part identifier "Comparison.Id" could not be bound.
The multi-part identifier "Baseline.Setup_Id" could not be bound.
The multi-part identifier "Setups.Id" could not be bound.
The multi-part identifier "BuildVersions.Id" could not be bound.
The multi-part identifier "Baseline.Status" could not be bound.
You’ve got mismatches between the aliases you’re specifying (e.g. in
Results AS ComparisonResult) and the aliases you’re trying to use (e.g. inComparison.Id). So, change this:to either this:
or this:
(and similarly for all the other joins).