title: SQL Server query related to grouping and counting for inventory analysis
body: <p>My table is concerned with inventory items, stocking levels and required numbers. An example is given below:</p>
  
  <pre><code>Part     Sub Part         Pieces Required        Pieces In Stock
  
  Barbie    Legs                 2                      1000
  Barbie    Arms                 2                      5000
  Barbie    Head                 1                       20
  Barbie    Torso                1                     40000
  Dora      Legs                 2                      1000
  Dora      Arms                 2                      5000
  Dora      Head                 1                       0
  Dora     Torso                 1                     40000
  </code></pre>
  
  <p>I want my end result to look like:</p>
  
  <pre><code>Part         No: of dolls that can be built
  
  Barbie           20
  Dora              0
  </code></pre>
  
  <p>So the logic is we need a minimum number of each part to make a complete doll. If even one of the required parts is not in stock, then no doll can be made. The complexity comes when we need 2 of certain parts and only 1 of other parts. 
  How do I achieve this using SQL Server? </p>
  
  <p>Thank you in advance for your help and time.</p>