How can I access an attribute in a subquery using the WHERE clause of an outer query?

How can I filter via an attribute that exists in a subquery using the WHERE clause of an outer query using TSQL / SQL Server 2008? The query was pretty long so I only included some of the code. I am getting an invalid column name for Sub.MemberStatusID=1. I am 100% sure that MemberStatusID exists in the table I am referencing.

SELECT
       S.StateName   AS StateName,
       A.StateId        AS StateId,
       C.CountyName AS CountyName
FROM
       (SELECT
                M.MemberID AS MMemberID,
                COALESCE(M.Number, R.Number) AS MyId
       FROM
               MTable AS M WITH(READUNCOMMITTED) FULL OUTER JOIN
               RTable AS R WITH(READUNCOMMITTED) ON M.MNumber=R.MNumber) AS Sub

LEFT JOIN MOTable AS MO WITH(READUNCOMMITTED) ON Sub.MMemberID=MO.MemberID
WHERE
         Sub.MemberStatusID=1 -- this is where the problem is at
....;
pae2Asked:
Who is Participating?
 
Chris MangusDatabase AdministratorCommented:
SELECT
       S.StateName   AS StateName,
       A.StateId        AS StateId,
       C.CountyName AS CountyName
FROM
       (SELECT
                M.MemberID AS MMemberID, MemberStatusID
                COALESCE(M.Number, R.Number) AS MyId
       FROM  MTable AS M WITH(READUNCOMMITTED)
       FULL OUTER JOIN  RTable AS R WITH(READUNCOMMITTED) ON M.MNumber=R.MNumber
       ) AS Sub

LEFT JOIN MOTable AS MO WITH(READUNCOMMITTED) ON Sub.MMemberID=MO.MemberID
WHERE Sub.MemberStatusID=1
0
 
Ephraim WangoyaCommented:

SELECT
       S.StateName   AS StateName,
       A.StateId        AS StateId,
       C.CountyName AS CountyName
FROM
       (SELECT
                M.MemberID AS MMemberID,
                COALESCE(M.Number, R.Number) AS MyId
       FROM  MTable AS M WITH(READUNCOMMITTED)
       FULL OUTER JOIN  RTable AS R WITH(READUNCOMMITTED) ON M.MNumber=R.MNumber
       WHERE MemberStatusID=1) AS Sub

LEFT JOIN MOTable AS MO WITH(READUNCOMMITTED) ON Sub.MMemberID=MO.MemberID
 
0
 
pae2Author Commented:
ewangoya, thanks, but I'm not looking to filter MemberStatusID using the inner WHERE clause. Can it be done using the WHERE clause of the outer query? If not, then I will use the inner WHERE clause. But I first want to know whether I can filter using the outer WHERE clause or not. Is it possible? If yes, please show me how! And if not, why can't this be done using the inner WHERE clause? Thanks!
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
pae2Author Commented:
cmangus, that's the exact code that I posted with my question. I am getting an invalid column name for Sub.MemberStatusID=1 with that code.
0
 
Chris MangusDatabase AdministratorCommented:
It's not the same code.  You didn't have MemberStatusID in the inner query, which I included above.
0
 
pae2Author Commented:
Thanks cmangus! I was missing the fact that I needed the attribute in both the inner and outer queries!
0
 
pae2Author Commented:
Thanks cmangus!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.