I have a requirement to group every N rows within each group of a SQL table, the best answer I've found is this; https://stackoverflow.com/a/66806186/10916933 but my data is also grouped by another identifier (Batch) and I would like to start a new GroupID every time the identifier changes. Batches are not of a consistent size. This is what I get currently (N = 2 for display purposes, it is actually 1000): Batch Value RowID GroupIDA 10.2 1 1A 6.7 2 1A 7.6 3 2B 7.0 4 2B 10.2 5 3C 9.6 6 3C 8.2 7 4C 1.1 8 4C 0.3 9 5C 9.0 10 5C 10.2 11 6 This is what I want (i.e. when Batch B starts, it gives me a new GroupID even though there has only been one): Batch Value RowID GroupIDA 10.2 1 1A 6.7 2 1A 7.6 3 2B 7.0 4 3B 10.2 5 3C 9.6 6 4C 8.2 7 4C 1.1 8 5C 0.3 9 5C 9.0 10 6C 10.2 11 6 Continue reading...