I often need to divide recordsets into groups for various reasons. For example, if we’re testing an application, the testers might want a census of sample users they’re going to test with, and I want to assign each sample user to a test group to ensure that they’re only tested once. This can be done using windowing in SQL Server (windowing was introduced in SQL Server 2005).
Here’s an example:
There are probably more elegant solutions, and the code looks a little messy, but it works.