title: The multi-part identifier could not be bound
body: <p>I've seen similar errors on SO, but I don't find a solution for my problem.
  I have a SQL query like:</p>
  
  <pre><code>SELECT DISTINCT
          a.maxa ,
          b.mahuyen ,
          a.tenxa ,
          b.tenhuyen ,
          ISNULL(dkcd.tong, 0) AS tongdkcd
  FROM    phuongxa a ,
          quanhuyen b
          LEFT OUTER JOIN ( SELECT    maxa ,
                                      COUNT(*) AS tong
                            FROM      khaosat
                            WHERE     CONVERT(DATETIME, ngaylap, 103) BETWEEN 'Sep 1 2011'
                                                                AND
                                                                'Sep 5 2011'
                            GROUP BY  maxa
                          ) AS dkcd ON dkcd.maxa = a.maxa
  WHERE   a.maxa &lt;&gt; '99'
          AND LEFT(a.maxa, 2) = b.mahuyen
  ORDER BY maxa;
  </code></pre>
  
  <p>When I execute this query, the error result is:
  <strong>The multi-part identifier "a.maxa" could not be bound.</strong> Why?
  <br>
  P/s: if i divide the query into 2 individual query, it run ok.</p>
  
  <pre><code>SELECT DISTINCT
          a.maxa ,
          b.mahuyen ,
          a.tenxa ,
          b.tenhuyen
  FROM    phuongxa a ,
          quanhuyen b
  WHERE   a.maxa &lt;&gt; '99'
          AND LEFT(a.maxa, 2) = b.mahuyen
  ORDER BY maxa;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>SELECT  maxa ,
          COUNT(*) AS tong
  FROM    khaosat
  WHERE   CONVERT(DATETIME, ngaylap, 103) BETWEEN 'Sep 1 2011'
                                          AND     'Sep 5 2011'
  GROUP BY maxa;
  </code></pre>