Isn't this the bin packing problem? Hugo Kornelis did a series of posts about it...
http://sqlblog.com/blogs/hugo_kornelis/archive/tags/Bin+Packing/default.aspx
Similar, but not looking for the efficiency of the lowest number of bins. I suspect the answer will be some adaptation of one of the methods Hugo presents.
First bin first / first fit method?
https://www.simple-talk.com/sql/t-sql-programming/bin-packing-problems-the-sql/ - there are a few typos in this article, though, so go carefully through it...
Is it important to batch items contiguously? Also is it important to batch them the most efficient way (fewest batches). Could a possible solution be: Batch 1 (Docs 1&2) = 32+25 = 57 Batch 2 (Docs 3&5) = 41+19 = 60 Batch 3 (Doc 4) = 82 Batch 4 (Doc 6) = 81 In your example it just so happens that when you keep the documents in the right order, you get an efficient batching.
Thats true.. unfortunately the batches i need should be contiguous. The method i used is not great ... i am looking for a better efficient way because I need to deal with a huge volume of data.