1

I'm looking to group rows together based on a set value. so I have a query that has 100 rows I would like to group them by every 10 rows and assign a number starting with 1.

So

First 10 rows would be assigned 1, next 10 get 2 next 10 get 3 and so on.

I have tried working this out with row_number, rank etc but not been able to do it.

is this possible?

Cheers Patrick

  • Possible duplicate of [In SQL, how can you "group by" in ranges?](https://stackoverflow.com/questions/232387/in-sql-how-can-you-group-by-in-ranges) – Hiko Haieto May 27 '17 at 20:06

1 Answers1

2

Based on ROW_NUMBER and integer division:

(Row_Number () Over (ORDER BY whatever) -1) / 10 + 1
dnoeth
  • 59,503
  • 4
  • 39
  • 56