title: The multi-part identifier could not be bound in a multiple condition where statement
body: <p>I'm trying to add an additional WHERE condition with AND to my mySQL query but keep getting the error <strong>The multi-part identifier userR.externalSource could not be bound</strong>
  I have the alias setup in my left join from my other table but still I get this error. Could someone explain me why this is happening. I found <a href="https://database.guide/sql-server-error-4104-the-multi-part-identifier-could-not-be-bound/" rel="nofollow noreferrer">article</a> but this does not seem to be the case. I think it's rather a formatting issue of my query.</p>
  <p>This query just counts 2 different groups of users but they can't have an empty (NULL) field for the externalSource field from the dbo.AAA_users table.</p>
  <pre><code>SELECT SUM(A.ROLECOUNT) AS 'Advanced Users' , 
      (SELECT SUM(A.ROLECOUNT)
      FROM (
          SELECT role.logicalName AS Role_name , 
                  COUNT(DISTINCT users.userId) AS RoleCount  
          FROM dbo.AAA_UserRoleResource AS users
              LEFT JOIN dbo.AAA_Role AS role ON role.roleId = users.roleId
              LEFT JOIN dbo.AAA_User AS userR ON userR.userId = users.userId
          GROUP BY role.logicalName
          ) A
      WHERE A.Role_name IN ('ROLE_VIEWER', 'ROLE_USER') 
      AND userR.externalSource is not NULL &lt;-- issue here  
      ) AS 'Basic user'
  FROM(
      SELECT role.logicalName AS Role_name , 
              COUNT(DISTINCT users.userId) AS RoleCount 
      FROM dbo.AAA_UserRoleResource AS users
          LEFT JOIN dbo.AAA_Role AS role ON role.roleId = users.roleId
          LEFT JOIN dbo.AAA_User AS userR ON userR.userId = users.userId
      GROUP BY role.logicalName
      ) A 
  WHERE A.Role_name IN ('ROLE_ADMIN');
  </code></pre>
  <p>Any help appreciated.</p>