title: Result set based off quantity of subquery
body: <p>I have two queries that I am trying to combine; basically the first query finds out how many items of specific product has been dispatched</p>
  
  <pre><code>SELECT 
      T0.SOPOrderReturnLineID,
      T1.Quantity AS 'QtyDespatched',
      T0.LineQuantity As 'OrderQty',
      T1.SopLinkId AS 'DespatchSopLinkId',
      T1.ItemCode 
  FROM
      SOPOrderReturnLine AS T0
  LEFT JOIN 
      DespatchedItems AS T1 ON T0.SOPOrderReturnLineID = T1.SopLinkId
  WHERE
      T1.SopLinkId IS NOT NULL AND
      T1.ItemCode = 'GSNOBAS60GW'
  GROUP BY 
      T1.SopLinkId, T0.SOPOrderReturnLineID, 
      T1.Quantity, T0.LineQuantity, T1.ItemCode
  </code></pre>
  
  <p>Here is a sample result set of the both queries</p>
  
  <p><a href="https://i.stack.imgur.com/z77pS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z77pS.png" alt="ql"></a></p>
  
  <p>This second query finds out how many of that item has been made in total by summing the quantities I don't want the items appearing in the above result set until the <code>OrderQty</code> match the 3 that has been made so far how is this done ?.</p>
  
  <pre><code>SELECT 
      SUM(CONVERT(INT, T1.Quantity))
  FROM 
      SOPOrderReturnLine AS T0
  LEFT JOIN 
      DespatchedItems AS T1 ON T0.SOPOrderReturnLineID = T1.SopLinkId
  WHERE
      T1.SopLinkId IS NOT NULL AND
      T1.ItemCode = 'GSNOBAS60GW'
  </code></pre>