title: Error when joining multiple table
body: <pre><code>SELECT 
      refnum as pornum,  tmp_Extract.matnum, matm.matmdescription,
      matm.extradescription, 
      SUM(sqty) AS porqty, CAST(SUM(dqty) AS numeric(16,4)) AS grnqty,   
      (sum(sqty) - sum(dqty)) AS balqty,
      (sum(sorqty) - sum(dorqty)) as balbaseqty,
      puom, pcfactor, apnum   
  FROM 
      (SELECT  
           matnum, matmdescription1, isnull(pqty,0) AS sqty, 0 AS dqty,   
           refnum, isnull(qty, 0) as sorqty, 0 as dorqty, 
           puom, pcfactor, apnum, porextractmark 
       FROM 
           por1  
       WHERE
           ((por1.clearflag IS NULL OR por1.clearflag = 0) 
           AND (por1.status &lt;&gt; 'V'))  
  </code></pre>
  
  <p>UNION ALL<br>
                SELECT deliverydate FROM porh
                where ((porh.flag ='POR')) and ((porh.status='P'))</p>
  
  <p>UNION ALL   </p>
  
  <pre><code>     SELECT  
           matnum, matmdescription1, 0 AS sqty, isnull(pqty / porpcfactor, 0) AS dqty, 
           pornum AS refnum, 0 as sorqty, isnull(qty,0) as dorqty,    
           puom, porpcfactor as pcfactor, apnum, porextractmark   
       FROM 
           dbo.grn1       
       WHERE
           grn1.refnum NOT IN (SELECT refnum 
                               FROM grnh 
                               WHERE grnh.status = 'v')) AS tmp_Extract  
  LEFT JOIN (
  (SELECT matnum, MAX(deliverydate) as deliverydate FROM porh GROUP BY matnum) 
  AS tmp_Extract
  
  LEFT JOIN matm on matm.matnum = tmp_Extract.matnum 
  where refnum like '%POR%'
  GROUP BY refnum, tmp_Extract.matnum,tmp_Extract.puom,tmp_Extract.pcfactor,
  apnum,matm.matmdescription,matm.extradescription,
  tmp_Extract.deliverydate
  having sum(sqty)&gt;sum(dqty)                                         
  </code></pre>
  
  <p>I want to join deliverydate from table <code>porh</code> but cannot..anyone can help?</p>