title: mysql; iterate through a table and calculate avgerage price
body: <p>I have the following query from a podetail table where I calculate the average price for "one" unique item at a time. I need to create a list of "avgUnitCost".Is there a way to use this existing query in a subquery to produce the list of avgUnitCost? </p>
  
  <pre><code>SELECT  items.`itemName`,
          SUM(podetails.`qtyReceived`) AS qtyRcvd,
          SUM(podetails.`qtyReceived` * podetails.`unitCost`) AS totalUnitCost,
          SUM(podetails.`qtyReceived` * podetails.`unitCost`) / SUM(podetails.`qtyReceived`) AS avgUnitCost
  
  FROM podetails, items, purchaseorders
  WHERE purchaseorders.`poID` = podetails.`poID`
    AND podetails.`itemID` = items.`itemID`
    AND podetails.`itemID` = 3;&gt;
  </code></pre>