My question is somewhat similar to
Group by every N records in T-SQL
however I want to group by a maximum dollar amount
So I have 20 records with a total amount
of 50,000, I need to break them into groups so that each group has a maximum amount of 10,000.
I tried using a running value, giving the records a rank and them summing the values <= the current rank, then breaking them into SubGroups using Floor((RunningValue - amount) / 10000)
, but there are circumstances where it goes over the 10,000 maximum
Sample data, you can see here that SubGroup 1 is over 10,000
SubGroupNo RowNo amount RunningValue
0 1 790.5 790.5
0 2 790.5 1581
0 3 790.5 2371.5
0 4 790.5 3162
0 5 744 3906
0 6 744 4650
0 7 1348.5 5998.5
0 8 1348.5 7347
0 9 1348.5 8695.5
1 10 1348.5 10044
1 11 1302 11346
1 12 1302 12648
1 13 1302 13950
1 14 1302 15252
1 15 1255.5 16507.5
1 16 1209 17716.5
1 17 1116 18832.5
1 18 1116 19948.5
2 19 1302 21250.5
2 20 1302 22552.5
2 21 1302 23854.5
2 22 1255.5 25110
2 23 1255.5 26365.5
2 24 976.5 27342