I am getting strange results from inner joins. Here is the SQL:
SELECT cm.PersonID
FROM dbo.vwCommitteeMembers cm
-- first join
INNER JOIN dbo.vwCommitteeTerms ct
ON (ct.CommitteeID = cm.CommitteeID)
-- second join
INNER JOIN dbo.vwCommitteeTermMembers ctm
ON (ct.ID = ctm.CommitteeTermID)
WHERE cm.CommitteeID = 124
If I comment out both joins, I get 24 results. If I uncomment out the first join (leaving second commented out), I also get 24 results. If, however, both joins are uncommented, the results are inflating to 576. Since 24^2 = 576, I have some idea what is happening, but I don’t know why. I am using SQL Server Management Studio 2008, on a Windows 7 Pro x64 workstation, for a SQL Server 2008 database. It “feels” like SSMS is trying to optimize my query somehow, but I really want to know what the root cause is so I don’t have this problem in the future (as this is not the first time I’ve encountered it). I even had a colleague take a look since I assumed I was just “too close” to the problem, and they thought it looked okay, too.
Thanks!
Lamak’s second comment was correct. Expected results are produced when the query is modified to: