SQL AZURE – Group records or widgets in Sets of a Specific Number

Useful if you have factory products that are being packed into sets of a specfic number. In our case sixes.

Swap out to a different amount if you need to. The last group in the record set will have a remainder level if the recordset is not perfectly divisible by the set integer.

SELECT pkid,
floor(((ROW_NUMBER() OVER(ORDER BY pkid))-1)/6)+1 as grpNum
FROM t002caskfill

Here is a screenshot of the above code being run on a table.