title: SQL - The multi-part identifier could not be bound
body: <p>I have an SQL query that I am trying to edit.  It returns the error :</p>
  
  <blockquote>
    <p>"The multi-part identifier "i.LastPurPrc" could not be bound." when I
    try to add a column 'Amount1'. (Error on 2nd line of query code)</p>
  </blockquote>
  
  <h2>The query:</h2>
  
  <pre><code>Select a.Itemcode, max(a.Dscription) as ItemName,
  sum(a.OpeningBalance) as OpeningBalance, sum(a.OpeningBalance) * i.LastPurPrc AS 'Amount1', sum(a.INq) as 'IN', sum(a.OUT) as OUT,
  ((sum(a.OpeningBalance) + sum(a.INq)) - sum(a.OUT)) as Closing,
  (Select i.InvntryUom from OITM i 
  Where i.ItemCode = a.Itemcode) as UOM
  from 
  (Select N1.Warehouse, N1.Itemcode, N1.Dscription, (sum(N1.inqty)-sum(n1.outqty))
  as OpeningBalance, 0 as INq, 0 as OUT 
  from dbo.OINM N1
  Where N1.DocDate &lt; '04-01-2015' and N1.Warehouse = 'WNR02' 
  Group By N1.Warehouse,N1.ItemCode,
  N1.Dscription 
  Union All 
  Select N1.Warehouse, N1.Itemcode, N1.Dscription, 0 as OpeningBalance,
  sum(N1.inqty), 0 as OUT 
  from dbo.OINM N1 
  Where N1.DocDate &gt;= '04-01-2015' and N1.DocDate &lt;= '04-30-2015'
  and N1.Inqty &gt; 0 and N1.Warehouse = 'WNR02' 
  Group By N1.Warehouse, N1.ItemCode, N1.Dscription
  Union All 
  Select N1.Warehouse, N1.Itemcode, N1.Dscription, 0 as OpeningBalance, 0 , sum(N1.outqty) as OUT
  From dbo.OINM N1 
  Where N1.DocDate &gt;= '04-01-2015' and N1.DocDate &lt;= '04-30-2015' and N1.OutQty &gt; 0
  and N1.Warehouse = 'WNR02' 
  Group By N1.Warehouse,N1.ItemCode,N1.Dscription) a, dbo.OITM I1
  where a.ItemCode = I1.ItemCode
  Group By a.Itemcode Having sum(a.OpeningBalance) + sum(a.INq) + sum(a.OUT) &gt; 0 Order By a.Itemcode
  </code></pre>
  
  <p>How do I solve this?</p>