问题
I have table like this
col1 col2 col3
3 5 8
4 5 5
5 5 5
3 3 3
4 5 6
I need to get table like below in SQL Server
col1 col2 col3 group
3 5 8 1
4 5 5 1
5 5 5 2
3 3 3 2
4 5 6 3
After some row count (say 25000 ) group column row count has to increase
(ex- if row count crosses 25,000 the group column value has to change to next number ie 25,001 - 2, 50001 - 3)
How to write a query in SQL Server?
回答1:
You can use row_number to generate numbers and the do some calculations.
This will make on group of 5 rows.
select Column1,
Column2,
1 + ((row_number() over(order by Column3) - 1) / 5)
from YourTable
来源:https://stackoverflow.com/questions/9733642/increase-row-count-in-sql-server-after-some-count-say-25-000