title: Divide Total Values Into Three Parts - Bin Packing
body: <p>I am trying to divide the second column into three parts, such that they are equally divided, but if the total values is not divisible by three, the case is that that first part is higher or equal than the second part, and the second part is higher or equal than the third part.</p>
  
  <p>Here is the sample of my table:</p>
  
  <pre><code>  ID   |  Values  
  -------------------------
     1       1
     2       1
     3       2
     4       1
     5       2
     6       1
     7       3
  </code></pre>
  
  <p>My expected output should be (The ID column here is just a row number):</p>
  
  <pre><code>  ID   |  Divided
  -------------------------
     1       3
     2       3
     3       1
  </code></pre>
  
  <p>As you can see, the total values is 11 if you add them all. So if we divide it by 3, the resulting values are 4, 4, and 3. So I will just count it to get the expected output:</p>
  
  <pre><code>(1, 1, 2) = 3, 
  next is (1, 2, 1) = 3, 
  and the last is (3) = 1.
  </code></pre>
  
  <p>Any idea how will I approach this problem? I have divided them by three but counting them to get the specific business logic is difficult:</p>
  
  <pre><code>select @intSum = SUM(Values) / 3 from @table
  </code></pre>
  
  <p>I am trying to this on stored procedure.</p>