Maybe something like this ?
First assigning a Row id for each row(from a query or static data and then joining to your original set), and working with that type of logic in a transform node.
This code will group values of 7 in the first 4 outputs, and the remainder in the 5th. After that you can use an aggregation node maybe.

Br
------------------------------
Henrik B
E.ON Sverige
------------------------------