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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.