title: Summarizing Only Rows with given criteria
body: <p>all!
  Given the following table structure </p>
  
  <pre><code>DECLARE @TempTable TABLE
  (
      idProduct INT, 
      Layers INT, 
      LayersOnPallet INT, 
      id INT IDENTITY(1, 1) NOT NULL, 
      Summarized BIT NOT NULL DEFAULT(0)
  )
  </code></pre>
  
  <p>and the following insert statement which generates test data</p>
  
  <pre><code>INSERT INTO @TempTable(idProduct, Layers, LayersOnPallet)
  SELECT 1, 2, 4
  UNION ALL
  SELECT 1, 2, 4
  UNION ALL
  SELECT 1, 1, 4
  UNION ALL
  SELECT 2, 2, 4
  </code></pre>
  
  <p>I would like to summarize only those rows (by the Layers only) with the same idProduct and which will have the sum of layers equal to LayersOnPallet.</p>
  
  <p>A picture is worth a thousand words:</p>
  
  <p><a href="https://i.stack.imgur.com/FvR88.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FvR88.png" alt="enter image description here"></a></p>
  
  <p>From the picture above, you can see that only the first to rows were summarized because both have the same idProduct and the sum(layers) will be equal to LayersOnPallet.
  How can I achieve this? It's there any way to do this only in selects (not with while)?</p>
  
  <p>Thank you!</p>